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 code snippet for computing the maximum path sum in a binary tree. Given the tree with root value 1, left child 2, and right child 3, what is the final returned maximum path sum?
easy
A. 3
B. 6
C. 5
D. 4

Solution

  1. Step 1: Trace helper on left child (2):

    left and right children are null, so left=0, right=0, max_sum updated to max(-inf, 2+0+0)=2, returns 2+max(0,0)=2.
  2. Step 2: Trace helper on right child (3):

    Similarly, max_sum updated to max(2, 3+0+0)=3, returns 3+max(0,0)=3.
  3. Step 3: Trace helper on root (1):

    left=2, right=3, max_sum updated to max(3, 1+2+3)=6, returns 1+max(2,3)=4.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Max path is 2 -> 1 -> 3 with sum 6 [OK]
Hint: Sum includes node plus max gains from both children [OK]
Common Mistakes:
  • Returning sum of one child only
  • Off-by-one in max_sum update
  • Ignoring zero cutoff for negative gains
2. Consider the following code snippet implementing the optimal solution for Word Search II. Given the board = [['o','a','a','n'], ['e','t','a','e'], ['i','h','k','r'], ['i','f','l','v']] and words = ['oath', 'pea', 'eat', 'rain'], what is the content of the result list res after the function completes?
easy
A. ['oath', 'pea', 'eat', 'rain']
B. ['pea', 'eat']
C. ['oath', 'eat', 'rain']
D. ['oath', 'eat']

Solution

  1. Step 1: Trace found words on board

    'oath' can be formed starting at (0,0) -> o-a-t-h; 'eat' can be formed starting at (1,1) -> e-a-t. 'pea' and 'rain' cannot be formed.
  2. Step 2: Confirm duplicates avoided

    Code sets found word to None to avoid duplicates, so only unique found words remain.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Only 'oath' and 'eat' are found on the board [OK]
Hint: Only words fully traceable on board appear [OK]
Common Mistakes:
  • Including words not on board
  • Duplicates in result
  • Missing found words
3. The following code attempts to solve the Russian Doll Envelopes problem optimally. Identify the line containing the subtle bug that causes incorrect results on some inputs.
medium
A. Line 4: Sorting envelopes with key (x[0], x[1]) ascending both
B. Line 6: Extracting heights from envelopes
C. Line 8: Using bisect_left to find insertion index
D. Line 11: Replacing tails element at found index

Solution

  1. Step 1: Examine sorting key

    Sorting by (width asc, height asc) allows envelopes with same width and increasing height, which breaks strict nesting rules.
  2. Step 2: Correct sorting key

    Sorting by (width asc, height desc) prevents envelopes with equal width but increasing height from being counted incorrectly.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Changing sorting to (x[0], -x[1]) fixes the bug [OK]
Hint: Sort height descending for equal widths to avoid counting duplicates [OK]
Common Mistakes:
  • Sorting both width and height ascending
  • Using <= instead of < in comparisons
  • Not handling equal widths properly
4. Suppose the Alien Dictionary problem is extended so that words can be repeated multiple times in the input list, possibly introducing duplicate edges. Which modification to the optimal Kahn's algorithm approach correctly handles this scenario without corrupting the in-degree counts?
hard
A. Use a set to track edges and only add an edge and increment in-degree if it is not already present
B. Reset in-degree counts after building the graph to avoid duplicates
C. Remove the edge deduplication check and increment in-degree for every edge found
D. Use DFS instead of BFS to avoid counting duplicate edges

Solution

  1. Step 1: Identify problem with duplicate edges

    Repeated words can cause multiple identical edges, inflating in-degree counts incorrectly.
  2. Step 2: Use edge deduplication to prevent multiple increments

    Track edges in a set and only add/increment if edge is new, preserving correct in-degree.
  3. Step 3: Confirm BFS with correct in-degree works properly

    Maintains accurate topological order and cycle detection despite duplicates.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Edge deduplication is essential for correctness with repeated words [OK]
Hint: Avoid incrementing in-degree multiple times for same edge [OK]
Common Mistakes:
  • Ignoring duplicates
  • Resetting in-degree incorrectly
  • Switching to DFS unnecessarily
5. Suppose the problem is modified so that the same letter cell on the board can be used multiple times within the same word search path (i.e., reuse of cells is allowed). Which modification to the optimal backtracking with Trie approach correctly handles this variant without causing infinite loops?
hard
A. Use a separate visited set to track cells per path and allow revisiting after backtracking
B. Remove the in-place marking of visited cells and rely solely on Trie pruning
C. Keep the in-place marking but reset visited cells only after exploring all neighbors
D. Modify the Trie to store counts of allowed cell reuse and decrement on each visit

Solution

  1. Step 1: Understand reuse impact on visited marking

    Allowing reuse means cells can be revisited, so marking cells as visited must be carefully managed to avoid infinite loops.
  2. Step 2: Identify correct marking strategy

    Using a separate visited set per path allows revisiting cells after backtracking, preventing infinite loops while supporting reuse.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Separate visited set per path safely handles cell reuse [OK]
Hint: Track visited cells per path to allow safe reuse [OK]
Common Mistakes:
  • Removing marking causes infinite loops
  • Using global visited set without reset
  • Modifying Trie for cell reuse incorrectly
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