Home > Reflections | ⏮️ ⏭️

# 2024-06-19

## 🧠 Education

📜 Don’t Get Down Leveled or How to Tell a Good Story (From a Principal at Amazon)

## ✍️ Writing Behavioral Stories

- Yesterday, I started brain dumping a timeline of recent work experiences that may make for good behavioral stories.
- I’m trying to focus on experiences that involve
- multiple, cross functional teams, and
- conflict resolution

- Today, I drafted a story from one of these experiences, applying the man in a hole story structure.
- My story rambled a bit on first draft, but while summarizing, I think I came up with some good bullet points.
- Honestly, it’s not my favorite kind of prep work, but I think if I keep devoting time to story crafting, I’ll wind up with some good fodder for interviews.

## 🏋️ Coding Practice

### 2235. Add Two Integers

Given two integers

`num1`

and`num2`

, returnthe.sumof the two integers

#### 🪞 Reflections

- Perhaps they need a new category that’s easier than
`Easy`

. Maybe they should call this`Trivial`

. - I could see asking about adding numbers when built in types can’t hold the result, but the constraints were -100 → 100, so there really doesn’t seem to be anything to this.

#### ⌨️ My Solution

### 1051. Height Checker

A school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in

non-decreasing orderby height. Let this ordering be represented by the integer array`expected`

where`expected[i]`

is the expected height of the`ith`

student in line.

You are given an integer array`heights`

representing thecurrent orderthat the students are standing in. Each`heights[i]`

is the height of the`ith`

student in line (0-indexed).

Returnthenumber of indiceswhere`heights[i] != expected[i]`

.

#### 🪞 Reflections

- Another easy problem with an easy solution.
- Looking at the recommended solution, they note that sorting is required, but they suggest that the point is to implement the sorting algorithm.
- I haven’t implemented a sorting algorithm from scratch for some time, so I’ll probably come back and try that out here.

#### ⌨️ My Solution

#### 346. Moving Average from Data Stream

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.

Implement the`MovingAverage`

class:

`MovingAverage(int size)`

Initializes the object with the size of the window`size`

.`double next(int val)`

Returns the moving average of the last`size`

values of the stream.

#### 🪞 Reflections

- Timing was fine
- Approach was good
- Errors I’m not worried about
- forgetting to prefix with
`this.`

- I used it in 3/4 places. This was an obvious slip. Perhaps a more careful final reading should have caught it. That said, I don’t expect to be implementing classes in interviews very often. - Using
`Array.unshift`

instead of`Array.shift`

.- During an interview, I’d just mention which method I intended to use.
- I can never remember which I want between
`shift`

and`unshift`

. - I should probably come up with some kind of mnemonic.
`shift`

removes the first element from the array and shifts every other element 1 back.`unshift`

inserts an element at the beginning and shifts every other element 1 forward.- This is difficult to remember because elements are shifting in both cases, and shift doesn’t imply a direction.
- The end-of-array counterparts to these methods are
`push`

and`pop`

, which I never confuse. - I know that
`push`

inserts and element at the end and`pop`

removes one. - This is the abstract API of a stack and the methods do what they say they do.
- Why were the terms
`shift`

and`unshift`

used? - They’re obviously inverse operations, but which one inserts and which one removes?
`shift`

is to`pop`

as`unshift`

is to`push`

.- 🤔 How to remember this relationship…
- ✅
`push`

and`unshift`

both contain a`u`

. - ✅
`pop`

and`shift`

are the shorter words in their respective pairs. - ❌ You have to
`push`

before you can`pop`

. You have to`unshift`

before you can`shift`

(this language seems counter-intuitive in this respect).

- ✅
- 😆 Ah, the internet is great sometimes. Here’s a good one from stack overflow: “Easy to remember if you mentally drop the “f” in shift / unshift:
`shift`

*removes*elements and`unshift`

*adds*them :)” – Vicky Chijwani

- forgetting to prefix with
- The error I shouldn’t make during an interview: The moving average I computed used the window
`size`

as the denominator instead of using the length of the array. This computed the wrong value when we hadn’t yet seen`size`

elements. My test didn’t catch this because the example assumed we’d already seen so many values. Running another manual test would not have taken much time, and would have caught this bug. - Even though I got the problem wrong, timing was fine. Even if I’d taken the time to run another test, which probably would have caught my bug, this would have been an acceptable amount of time for this problem.

#### ⌨️ My Solution

### 704. Binary Search

Given an array of integers

`nums`

which is sorted in ascending order, and an integer`target`

, write a function to search`target`

in`nums`

. If`target`

exists, then return its index. Otherwise, return`-1`

.

You must write an algorithm with`O(log n)`

runtime complexity.

#### 🪞 Reflections

- I’m glad I finally got around to implementing this basic algorithm, as it should roll off my mind quickly if I need it as part of a larger solution during an interview, and there are a few tricky bits to be careful with.
- ✅ I found a bug during testing: we have to adjust the index inward by 1 to ensure we don’t infinite loop (due to rounding). After doing this, I also realized I should change my loop condition from
`!==`

to`<=`

to avoid my boundary indices missing each other and then looping forever. I’m not actually sure if that’s necessary here, but it’s a good safeguard. - It can be difficult to remember if it matters whether we use floor, ceiling, or rounding when the midpoint is a fraction. As long as we adjust the boundaries properly, it doesn’t matter. Here’s a good explanation on stack overflow.