Bird
Raised Fist0
Interview Prepchallenge-problemshardGoogleAmazonFacebook

Smallest Range Covering K Lists

Choose your preparation mode4 modes available
</>
IDE
def smallestRangeBruteForce(nums: list[list[int]]) -> list[int]:public int[] smallestRangeBruteForce(List<List<Integer>> nums)vector<int> smallestRangeBruteForce(vector<vector<int>>& nums)function smallestRangeBruteForce(nums)
def smallestRangeBruteForce(nums):
    # Write your solution here
    pass
class Solution {
    public int[] smallestRangeBruteForce(List<List<Integer>> nums) {
        // Write your solution here
        return new int[0];
    }
}
#include <vector>
using namespace std;

vector<int> smallestRangeBruteForce(vector<vector<int>>& nums) {
    // Write your solution here
    return {};
}
function smallestRangeBruteForce(nums) {
    // Write your solution here
}
Coming soon
0/10
Common Bugs to Avoid
Wrong: [1,3]Greedy approach picking minimum elements without ensuring coverage of all lists.✅ Use a min-heap to track current minimum element and advance pointers only when all lists are covered.
Wrong: [] or nullFailing to handle empty input or lists with zero elements gracefully.✅ Add input validation to return [] immediately if any list is empty or input is empty.
Wrong: [0,0]Incorrectly assuming zero range when lists do not overlap or missing to update range properly.✅ Update range only when all lists are covered and track min and max elements correctly.
Wrong: [4,10]Not minimizing the range properly, returning a valid but not minimal range.✅ Continuously update the range when a smaller range covering all lists is found.
✓
Test Cases
t1_01basic
Input{"nums":[[4,10,15,24,26],[0,9,12,20],[5,18,22,30]]}
Expected[20,24]

The range [20,24] includes 24 from the first list, 20 from the second list, and 22 from the third list, covering all lists with the smallest possible range.

t1_02basic
Input{"nums":[[1,5,8],[4,12],[7,8,10]]}
Expected[4,8]

The range [4,8] includes 5 from the first list, 4 from the second list, and 7 or 8 from the third list, covering all lists with the smallest possible range.

t2_01edge
Input{"nums":[]}
Expected[]

Empty input means no lists to cover, so no valid range exists; return empty list.

t2_02edge
Input{"nums":[[7]]}
Expected[7,7]

Single list with one element means the smallest range is that element itself.

t2_03edge
Input{"nums":[[-10,-5,0],[0,5,10],[10,15,20]]}
Expected[0,10]

Range [0,10] covers 0 from first and second lists and 10 from third list, crossing zero with negative and positive numbers.

t2_04edge
Input{"nums":[[100000],[100000],[100000]]}
Expected[100000,100000]

All lists contain the same large element; smallest range is that element itself.

t3_01corner
Input{"nums":[[1,2,3],[1,2,3],[1,2,3]]}
Expected[1,1]

All lists contain the same elements; smallest range is [1,1] covering all lists.

t3_02corner
Input{"nums":[[1,5,9],[2,6,10],[3,7,11]]}
Expected[3,7]

Greedy approach picking minimums fails; correct smallest range is [3,7] covering all lists.

t3_03corner
Input{"nums":[[1,2,3],[4,5,6],[7,8,9]]}
Expected[3,9]

Tests correct handling of disjoint lists and advancing pointers to find minimal range.

t4_01performance
Input{"nums":[[0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98],[1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75,77,79,81,83,85,87,89,91,93,95,97,99],[50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99],[-50,-49,-48,-47,-46,-45,-44,-43,-42,-41,-40,-39,-38,-37,-36,-35,-34,-33,-32,-31,-30,-29,-28,-27,-26,-25,-24,-23,-22,-21,-20,-19,-18,-17,-16,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1],[25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74],[10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59],[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49],[20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69],[30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79],[40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89]]}
⏱ Performance - must finish in 2000ms

n=10 lists with up to 50 elements each (total ~500 elements). O(n log n) or O(n log k) solution must complete within 2s. Brute force O(m^k) will TLE.

Practice

(1/5)
1. Consider the following Python code that merges k sorted arrays using divide and conquer. Given the input arrays [[1,4,5],[1,3,4],[2,6]], what is the value of merged_arrays after the first iteration of the while loop inside merge_k_sorted_arrays?
easy
A. [[1,1,3,4,4,5,2,6]]
B. [[1,1,3,4,4,5],[2,6]]
C. [[1,3,4,5],[1,2,4,6]]
D. [[1,1,3,4,4,5],[2,6]]

Solution

  1. Step 1: Trace merging pairs in first iteration

    Arrays are [[1,4,5],[1,3,4],[2,6]]. Merge first two: merge_two_arrays([1,4,5],[1,3,4]) -> [1,1,3,4,4,5]. The last array [2,6] remains as is because no pair.
  2. Step 2: Form merged_arrays after first iteration

    merged_arrays = [[1,1,3,4,4,5],[2,6]]
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    First pair merged correctly, last array unchanged -> matches [[1,1,3,4,4,5],[2,6]] [OK]
Hint: Merge pairs left to right, last array unchanged if odd count [OK]
Common Mistakes:
  • Merging last array incorrectly
  • Off-by-one in loop
  • Appending arrays instead of merging
2. Suppose the problem is modified so that elements from nums2 can be reused multiple times in pairs (i.e., pairs can reuse the same element from nums2 multiple times). Which modification to the min-heap approach correctly handles this variant?
hard
A. Push all pairs (nums1[i] + nums2[j]) for all i, j into the heap initially and pop k times.
B. After popping a pair (i, j), push (i, j+1) and also push (i+1, j) if within bounds, allowing reuse of nums2 elements.
C. After popping a pair (i, j), push (i, j) again to allow reuse of nums2[j].
D. Modify the heap to store sums with counts of usage and only push new pairs when usage count is less than k.

Solution

  1. Step 1: Understand reuse requirement

    Allowing reuse of nums2 elements means after using nums2[j], we can also try nums2[j+1] and nums2[j] again with next nums1 element.
  2. Step 2: Modify heap push logic

    After popping (i, j), pushing (i, j+1) continues along nums2, and pushing (i+1, j) allows pairing next nums1 element with the same nums2[j], enabling reuse.
  3. Step 3: Reject other options

    Push all pairs (nums1[i] + nums2[j]) for all i, j into the heap initially and pop k times. is brute force and inefficient. After popping a pair (i, j), push (i, j) again to allow reuse of nums2[j]. causes infinite loops by pushing same pair repeatedly. Modify the heap to store sums with counts of usage and only push new pairs when usage count is less than k. is unnecessarily complex and not standard.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    After popping a pair (i, j), push (i, j+1) and also push (i+1, j) if within bounds, allowing reuse of nums2 elements. correctly expands pairs to allow reuse without infinite loops [OK]
Hint: Push next nums1 with same nums2 to allow reuse [OK]
Common Mistakes:
  • Pushing same pair repeatedly causing infinite loops
  • Brute forcing all pairs initially
  • Overcomplicating usage counts
3. Suppose the array elements can be reused multiple times (duplicates allowed and repeated). Which modification to the k-sorted array sorting algorithm is necessary to correctly handle this scenario?
hard
A. No modification needed; the existing min-heap approach works correctly with duplicates.
B. Use a max-heap instead of a min-heap to handle duplicates correctly.
C. Add a frequency map to track counts and adjust heap operations accordingly.
D. Sort the array fully using a standard sort to handle duplicates reliably.

Solution

  1. Step 1: Understand duplicates in k-sorted array

    Duplicates do not affect the heap property; min-heap handles equal elements correctly.
  2. Step 2: Confirm no algorithmic change needed

    Heap operations remain valid; duplicates are naturally ordered by heapq.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Min-heap supports duplicates without extra data structures [OK]
Hint: Min-heap naturally supports duplicates without changes [OK]
Common Mistakes:
  • Thinking max-heap is needed for duplicates
  • Adding unnecessary frequency maps
  • Defaulting to full sort unnecessarily
4. If the problem changes so that super ugly numbers can be reused multiple times (i.e., duplicates allowed in the sequence), which modification to the min-heap approach is correct?
hard
A. Use a BFS approach with a queue instead of a heap to allow duplicates naturally.
B. Remove the duplicate check when appending values to ugly and push new heap elements as usual.
C. Keep the duplicate check but also maintain a global set to avoid pushing duplicates into the heap.
D. Modify the heap push to multiply primes with the last appended ugly number instead of indexed ugly numbers.

Solution

  1. Step 1: Understand the reuse variant

    Allowing duplicates means we no longer need to skip repeated values; all generated multiples are valid.
  2. Step 2: Adjust algorithm accordingly

    Removing the duplicate check when appending allows duplicates; heap push logic remains unchanged.
  3. Step 3: Verify correctness

    Heap still merges sequences correctly; duplicates appear naturally without filtering.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Removing duplicate check aligns with allowing reuse [OK]
Hint: Allow duplicates -> remove duplicate filtering, keep heap logic [OK]
Common Mistakes:
  • Keeping duplicate check breaks reuse requirement
  • Adding global sets wastes space unnecessarily
  • Changing multiplication logic breaks sequence order
5. Suppose the problem changes: now you want to find the nth ugly number but you can reuse the same ugly number multiple times in generating new numbers (i.e., duplicates allowed). Which modification to the min-heap approach correctly handles this variant without losing correctness or efficiency?
hard
A. Remove the 'seen' set entirely and push all generated numbers into the heap, allowing duplicates.
B. Keep the 'seen' set but reset it after every iteration to allow reuse of numbers in the next iteration.
C. Use a counter map to track how many times each number is generated and only push if count is below a threshold.
D. Keep the 'seen' set as is to avoid duplicates, since duplicates do not affect correctness or efficiency.

Solution

  1. Step 1: Understand the variant allowing duplicates

    Allowing reuse means duplicates are valid and should be generated and processed multiple times.
  2. Step 2: Modify the algorithm accordingly

    Removing the 'seen' set allows duplicates to be pushed and popped, correctly reflecting the new problem constraints.
  3. Step 3: Consider efficiency

    Though duplicates increase heap size, this is necessary to correctly generate all numbers under the new rules.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Removing 'seen' allows duplicates as required [OK]
Hint: Allow duplicates by removing 'seen' when reuse is allowed [OK]
Common Mistakes:
  • Keeping 'seen' and missing duplicates
  • Resetting 'seen' incorrectly
  • Using counters 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