Practice
1. Given the following Python code implementing Quickselect, what is the output of
findKthLargest([4,1,3,2], 3)?easy
Solution
Step 1: Understand k and indexing
k=3 means the 3rd largest element, so index k-1=2 in zero-based indexing.Step 2: Trace partition and quickselect
Partition rearranges so elements > pivot are left; after recursive calls, the element at index 2 is 2.Final Answer:
Option A -> Option AQuick Check:
3rd largest in [4,1,3,2] is 2 [OK]
Hint: Remember zero-based index k-1 for kth largest [OK]
Common Mistakes:
- Off-by-one error in k indexing
- Confusing kth largest with kth smallest
- Misunderstanding partition direction
2. You need to continuously track the kth largest element in a stream of integers where new elements arrive one by one. Which approach guarantees efficient updates and retrievals after each new element arrives?
easy
Solution
Step 1: Understand the problem constraints
We need to efficiently update and retrieve the kth largest element after each insertion in a stream.Step 2: Evaluate approaches
Sorting the entire stream each time (B) is inefficient (O(n log n) per add). Using a max-heap of all elements (C) requires popping k times per query, which is costly. Dynamic programming (D) is not applicable here as the problem is not about overlapping subproblems but about maintaining order dynamically. Maintaining a min-heap of size k (A) keeps the kth largest element at the root, allowing O(log k) updates and O(1) retrieval.Final Answer:
Option B -> Option BQuick Check:
Min-heap of size k efficiently tracks kth largest element [OK]
Hint: Min-heap size k keeps kth largest at root efficiently [OK]
Common Mistakes:
- Sorting entire stream each time is too slow
- Using max-heap of all elements wastes space and time
- Dynamic programming is irrelevant here
3. The following code attempts to find the kth smallest element in a sorted matrix using binary search on the value range. Identify the line containing the subtle bug that causes incorrect results on some inputs.
medium
Solution
Step 1: Examine countLessEqual condition
The condition uses '<' instead of '<=' which causes elements equal to mid to be excluded from count.Step 2: Understand impact on binary search
Excluding elements equal to mid causes countLessEqual to undercount, leading binary search to converge incorrectly.Final Answer:
Option A -> Option AQuick Check:
Changing '<' to '<=' fixes counting and correctness [OK]
Hint: Counting ≤ mid must include equality [OK]
Common Mistakes:
- Using '<' instead of '<=' in count function
- Off-by-one in binary search updates
4. What is the time complexity of the optimal bucket sort approach for sorting characters by frequency in a string of length n?
medium
Solution
Step 1: Count frequencies in O(n)
Counting frequencies requires one pass over the string.Step 2: Bucket sort in O(n + m)
Characters are placed into buckets indexed by frequency; iterating buckets takes O(m), total characters sum to n.Final Answer:
Option A -> Option AQuick Check:
Counting + bucket iteration both linear in n and max frequency m [OK]
Hint: Bucket sort complexity is O(n + m), where m is max frequency [OK]
Common Mistakes:
- Assuming sorting frequencies requires O(n log n)
- Confusing unique chars count with n
5. Suppose the problem changes: the matrix is no longer sorted by columns, only rows are sorted, and you still need to find the kth smallest element. Which approach is the most suitable to solve this variant efficiently?
hard
Solution
Step 1: Understand changed constraints
Only rows are sorted, columns are not sorted, so binary search counting on columns is invalid.Step 2: Identify suitable approach
Min heap merging k sorted lists (rows) efficiently finds kth smallest in O(k log n) time.Step 3: Reject other approaches
Binary search counting fails without column sorting; flattening is O(n² log n²); greedy is incorrect.Final Answer:
Option D -> Option DQuick Check:
Min heap merge exploits row sorting without column sorting [OK]
Hint: Without column sorting, binary search counting fails [OK]
Common Mistakes:
- Trying binary search counting without column sorting
- Assuming greedy works
