Bird
Raised Fist0
Interview Prepmodified-binary-searchmediumAmazonGoogleMicrosoft

Search a 2D Matrix II (Staircase)

Choose your preparation mode4 modes available
▶
Steps
setup

Start searching first row

Begin the search on the first row of the matrix. Initialize the row index to 0 and prepare to perform binary search on this row.

💡 Starting with the first row sets the stage for applying binary search row by row.
Line:for row in matrix:
💡 The algorithm will iterate over each row to find the target.
📊
Search a 2D Matrix II (Staircase) - Watch the Algorithm Execute, Step by Step
Watching each pointer move and comparison helps you understand how binary search efficiently narrows down the search space in a sorted row.
Step 1/17
·Active fill★Answer cell
setup
1
0
4
1
7
2
11
3
15
4
Result: "No rows searched yet"
setup
left
1
0
4
1
7
2
11
3
right
15
4
Result: "No rows searched yet"
compare
left
1
0
4
1
mid
7
2
11
3
right
15
4
Result: "No rows searched yet"
move_right
left
1
0
right
4
1
7
2
11
3
15
4
Result: "No rows searched yet"
compare
mid
1
0
right
4
1
7
2
11
3
15
4
Result: "No rows searched yet"
move_left
1
0
right
4
1
7
2
11
3
15
4
Result: "No rows searched yet"
compare
1
0
mid
4
1
7
2
11
3
15
4
Result: "No rows searched yet"
move_left
1
0
right
4
1
left
7
2
11
3
15
4
Result: "No rows searched yet"
setup
2
0
5
1
8
2
12
3
19
4
Result: "First row searched, target not found"
setup
left
2
0
5
1
8
2
12
3
right
19
4
Result: "First row searched, target not found"
compare
left
2
0
5
1
mid
8
2
12
3
right
19
4
Result: "First row searched, target not found"
move_right
left
2
0
right
5
1
8
2
12
3
19
4
Result: "First row searched, target not found"
compare
mid
2
0
right
5
1
8
2
12
3
19
4
Result: "First row searched, target not found"
move_left
2
0
right
5
1
8
2
12
3
19
4
Result: "First row searched, target not found"
compare
2
0
mid
5
1
8
2
12
3
19
4
Result: "First row searched, target not found"
compare
2
0
mid
5
1
8
2
12
3
19
4
Result: "Target found"
record
2
0
mid
5
1
8
2
12
3
19
4
Result: true

Key Takeaways

✓ Binary search efficiently narrows the search space by halving it each step.

This halving behavior is hard to visualize from code alone but becomes clear when watching pointer movements.

✓ The algorithm applies binary search independently on each row until the target is found.

Seeing the row-by-row iteration helps understand why the algorithm is still efficient despite multiple searches.

✓ Pointer updates depend on comparisons, and the search ends immediately when the target is found.

Watching the exact pointer moves and the early termination clarifies the control flow better than reading code.

Practice

(1/5)
1. Given the following code and input nums = [2, 4, 4, 6] and target = 4, what is the final returned value?
easy
A. [1, 2]
B. [2, 3]
C. [0, 1]
D. [-1, -1]

Solution

  1. Step 1: Trace binary search to find any occurrence of 4

    Initial left=0, right=3, mid=1, nums[1]=4 matches target, found=1.
  2. Step 2: Expand to find leftmost and rightmost indices

    Start=1, check left: nums[0]=2 !=4 stop; end=1, check right: nums[2]=4 matches, end=2; nums[3]=6 stop.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Positions of 4 are indices 1 and 2 [OK]
Hint: Break binary search on first found, then expand boundaries [OK]
Common Mistakes:
  • Off-by-one errors expanding boundaries
  • Returning found index only
  • Confusing indices with values
2. You need to compute x raised to the power n efficiently, where n can be a large integer (positive or negative). Which approach guarantees the optimal time complexity for this problem?
easy
A. Use binary exponentiation by repeatedly squaring x and reducing n by half.
B. Iteratively multiply x by itself n times in a loop.
C. Use dynamic programming to store intermediate powers and build up to x^n.
D. Use a greedy approach to multiply by the largest possible power of x at each step.

Solution

  1. Step 1: Understand the problem constraints

    We want to compute x^n efficiently for large n, including negative values.
  2. Step 2: Identify the optimal approach

    Binary exponentiation (fast exponentiation) reduces the number of multiplications by squaring x and halving n at each step, achieving O(log n) time.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Binary exponentiation is the classic optimal approach for power calculation [OK]
Hint: Binary exponentiation halves exponent each step [OK]
Common Mistakes:
  • Thinking iterative multiplication is efficient for large n
  • Confusing DP with binary exponentiation
  • Assuming greedy approach works for powers
3. What is the time complexity of the optimal binary search solution for the Minimum Days to Make m Bouquets problem, given n flowers and day range D = maxDay - minDay?
medium
A. O(n * D) because we check each day sequentially and scan all flowers.
B. O(n log D) because binary search reduces day range and each feasibility check scans n flowers.
C. O(log n * D) because binary search is on flowers and day range is scanned.
D. O(n + D) because we combine linear scans of flowers and days.

Solution

  1. Step 1: Identify outer loop complexity

    Binary search runs in O(log D) iterations, where D is the day range.
  2. Step 2: Identify inner loop complexity

    Each feasibility check scans all n flowers in O(n) time.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Combining gives O(n log D), not O(n * D) or others. [OK]
Hint: Binary search log factor times linear feasibility check [OK]
Common Mistakes:
  • Confusing linear scan over days as O(n*D)
  • Mixing binary search on flowers instead of days
4. Suppose the problem is modified so that packages can be shipped in any order (not necessarily the given order) within the given days. Which of the following approaches correctly finds the minimal ship capacity under this new constraint?
hard
A. Use dynamic programming to assign packages to days minimizing maximum load per day, combined with binary search on capacity
B. Sort packages in descending order and use a greedy bin-packing heuristic combined with binary search on capacity
C. Use a simple greedy approach assigning packages to days in any order without binary search
D. Use the same binary search on capacity with the original feasibility check since order doesn't matter

Solution

  1. Step 1: Understand the relaxed constraint

    Packages can be shipped in any order, so the problem becomes a variant of multi-way partition or bin packing minimizing max load.
  2. Step 2: Identify correct approach

    Binary search on capacity still applies, but feasibility check requires DP or backtracking to assign packages optimally to days, as greedy heuristics may fail.
  3. Step 3: Why other options fail

    Original feasibility check assumes fixed order; greedy heuristics are approximate and may not find minimal capacity.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    DP feasibility check with binary search needed for unordered assignment [OK]
Hint: Order relaxation requires DP feasibility check, not simple greedy [OK]
Common Mistakes:
  • Using original feasibility check ignoring order change
  • Assuming greedy suffices
  • Ignoring complexity increase
5. Suppose now you want to make bouquets but flowers can be reused multiple times (i.e., each flower can be counted multiple times for different bouquets). Which modification to the algorithm is correct?
hard
A. Keep the same binary search and feasibility check, but remove resetting flowers count to zero on encountering a flower that blooms after the day.
B. Binary search is no longer applicable; instead, use a greedy approach to pick flowers repeatedly until bouquets are formed.
C. Use binary search on days, but feasibility must count bouquets by grouping any flowers bloomed by day, allowing reuse by not resetting flowers count.
D. Modify feasibility to count total flowers bloomed by day and check if total flowers >= m * k, ignoring adjacency.

Solution

  1. Step 1: Understand reuse changes adjacency constraint

    If flowers can be reused multiple times, adjacency no longer matters; only total count of bloomed flowers by day matters.
  2. Step 2: Modify feasibility check

    Feasibility reduces to checking if total bloomed flowers by day >= m * k, ignoring adjacency and resetting.
  3. Step 3: Binary search remains valid

    Binary search on days still applies, but feasibility is simplified to total count check.
  4. Final Answer:

    Option D -> Option D
  5. Quick Check:

    Counting total bloomed flowers matches reuse scenario. [OK]
Hint: Reuse removes adjacency; feasibility counts total bloomed flowers [OK]
Common Mistakes:
  • Ignoring adjacency but still resetting flowers count
  • Removing binary search unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard