Bird
Raised Fist0
Interview Prepk-way-mergehardAmazonGoogle

Find Median of K Sorted Arrays

Choose your preparation mode4 modes available
▶
Steps
setup

Initialize total length and search range

Calculate the total number of elements across all arrays. Find the global minimum and maximum values to set the initial binary search range.

💡 Knowing the total length and value range is essential to perform binary search on the value domain instead of indices.
Line:total_len = sum(len(arr) for arr in arrays) min_val = min((arr[0] for arr in arrays if arr), default=0) max_val = max((arr[-1] for arr in arrays if arr), default=0)
💡 Establishes the search boundaries for the median value based on input arrays.
📊
Find Median of K Sorted Arrays - Watch the Algorithm Execute, Step by Step
Watching the binary search on value range with counting across arrays reveals how the median is found without merging all arrays explicitly.
Step 1/9
·Active fill★Answer cell
peekmin-heap
1
0
9
1
peekmin-heap
1
0
9
1
peekmin-heap
1
0
9
1
peekmin-heap
5
0
4
1
2
2
3
3
4
4
5
5
Added: 5
insertmin-heap
5
0
Added: 5
peekmin-heap
1
0
9
1
peekmin-heap
5
0
4
1
2
2
3
3
4
4
5
5
Added: 5
insertmin-heap
5
0
Added: 5
peekmin-heap
5
0
Added: 5

Key Takeaways

✓ Binary searching on the value range instead of merging arrays is efficient and elegant.

Reading code alone hides how the search narrows down the median value by counting elements ≤ mid.

✓ Counting elements ≤ mid across all arrays using binary search is the core operation guiding the search.

Visualizing each count clarifies why the search boundaries move up or down.

✓ The algorithm finds two median candidates (median1 and median2) to handle both even and odd total lengths.

Seeing both searches side by side helps understand how the median is computed precisely.

Practice

(1/5)
1. You are given k sorted lists of integers. Your task is to find the smallest range that includes at least one number from each of the k lists. Which algorithmic approach guarantees an optimal solution with efficient time complexity?
easy
A. Use a min-heap to track the current minimum element among the heads of each list while maintaining the current maximum, updating the range as you iterate.
B. Apply a greedy approach by always picking the smallest element from any list and expanding the range until all lists are covered.
C. Sort all elements from all lists into one array and use a dynamic programming approach to find the minimal range covering all lists.
D. Use a brute force approach enumerating all combinations of elements from each list and selecting the minimal range.

Solution

  1. Step 1: Understand the problem constraints

    The problem requires finding a range that covers at least one element from each of the k sorted lists, minimizing the range size.
  2. Step 2: Identify the optimal approach

    Using a min-heap to track the smallest current element from each list and maintaining the maximum element seen so far allows efficient updates of the range while iterating through the lists.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Min-heap approach efficiently tracks min and max for optimal range [OK]
Hint: Min-heap tracks min and max for optimal range [OK]
Common Mistakes:
  • Thinking brute force is feasible for large k
  • Assuming greedy picking smallest element always works
  • Confusing DP applicability here
2. Consider the following buggy code for finding the nth ugly number. Which line contains the subtle bug that causes incorrect results or performance issues?
medium
A. Line 3: heap initialization with [1]
B. Line 4: Missing initialization of the 'seen' set
C. Line 7: Loop runs n times popping from heap
D. Line 10: Multiplying ugly by factors 2, 3, and 5

Solution

  1. Step 1: Identify missing duplicate check

    The 'seen' set is not initialized and the check to avoid pushing duplicates is missing, causing duplicates to be pushed repeatedly.
  2. Step 2: Understand impact of missing 'seen'

    Without 'seen', the heap grows unnecessarily large with duplicates, causing performance degradation and possibly incorrect nth number due to repeated values.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Missing 'seen' set causes duplicates and performance issues [OK]
Hint: Always track seen elements to avoid duplicates in heap [OK]
Common Mistakes:
  • Forgetting to initialize or use 'seen'
  • Assuming heap automatically avoids duplicates
3. Suppose jobs can be reused multiple times (i.e., the same job can be assigned repeatedly), and you want to assign jobs to machines minimizing start delays as before. Which modification to the optimal algorithm is necessary to handle this scenario correctly?
hard
A. No change needed; the existing algorithm handles reuse naturally by sorting jobs once and assigning sequentially.
B. Sort jobs by duration instead of start time to prioritize shorter jobs for reuse.
C. Use a brute force approach checking all possible assignments since reuse breaks heap assumptions.
D. Maintain a priority queue of job instances and re-insert jobs after completion to simulate reuse, updating start times accordingly.

Solution

  1. Step 1: Understand job reuse impact

    Reusing jobs means after a job finishes, it can be assigned again, so jobs are not fixed once but can appear multiple times.
  2. Step 2: Modify algorithm to handle reuse

    Maintain a priority queue of job instances with their next available start times. After a job finishes, re-insert it with updated start time to simulate reuse.
  3. Step 3: Why other options fail

    No change ignores reuse; brute force is inefficient; sorting by duration ignores start times and scheduling constraints.
  4. Final Answer:

    Option D -> Option D
  5. Quick Check:

    Re-inserting jobs after completion models reuse correctly [OK]
Hint: Re-insert jobs with updated start times to simulate reuse [OK]
Common Mistakes:
  • Assuming reuse needs no algorithm change
  • Switching sort order breaks scheduling correctness
  • Using brute force is inefficient and unnecessary
4. Suppose you want to merge k sorted lists but now each list can contain duplicate values that must be preserved, and you want to return the top M smallest elements overall efficiently. Which modification to the standard divide and conquer approach is best?
hard
A. Continue using divide and conquer merging all lists fully, then slice the first M elements from the merged list.
B. Use dynamic programming to memoize merged prefixes and retrieve top M elements.
C. Concatenate all lists, sort them fully, then return first M elements.
D. Use a min heap to always extract the smallest current node among all lists until M elements are collected.

Solution

  1. Step 1: Understand the requirement for top M elements

    We only need the first M smallest elements, not the full merged list.
  2. Step 2: Analyze approaches for efficiency

    Divide and conquer merges all lists fully (O(N log k)), which is wasteful if M << N. Concatenation and sort is O(N log N). Min heap approach extracts smallest nodes one by one, stopping after M elements, achieving O(M log k).
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Min heap approach efficiently returns top M elements without full merge [OK]
Hint: Min heap extracts smallest nodes incrementally, ideal for top M [OK]
Common Mistakes:
  • Merging all lists fully then slicing wastes time
  • Sorting all nodes is inefficient for partial results
  • Dynamic programming is not applicable here
5. Suppose the problem is modified so that elements in the lists can be reused multiple times (i.e., you can pick the same element multiple times from the same list). Which modification to the optimal min-heap approach correctly handles this variant?
hard
A. Continue using the min-heap approach but do not advance the index in the list after pushing the next element, allowing reuse of the same element.
B. Use a min-heap but track counts of elements used per list and only advance indices when all elements have been used once.
C. Modify the algorithm to allow pushing the same element multiple times without advancing the index, but update current_max accordingly to avoid infinite loops.
D. The min-heap approach cannot be adapted; instead, use a dynamic programming approach to handle reuse.

Solution

  1. Step 1: Understand reuse implications

    Allowing reuse means the same element can be counted multiple times, so indices alone do not track usage.
  2. Step 2: Modify tracking mechanism

    Tracking counts of usage per list and advancing indices only after all elements are used once prevents infinite loops and ensures coverage.
  3. Step 3: Why other options fail

    Not advancing indices leads to infinite loops; pushing same element multiple times without control breaks correctness; DP is inefficient here.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Tracking usage counts ensures correct reuse handling [OK]
Hint: Track usage counts per list to handle reuse correctly [OK]
Common Mistakes:
  • Not advancing indices causing infinite loops
  • Pushing duplicates without control
  • Switching to DP 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