Practice
findKthLargest([4,1,3,2], 3)?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]
- Off-by-one error in k indexing
- Confusing kth largest with kth smallest
- Misunderstanding partition direction
max_load after processing all events for the input jobs = [[1,4,3], [2,5,4], [7,9,6]]?
def find_max_cpu_load(jobs):
events = []
for start, end, load in jobs:
events.append((start, load))
events.append((end, -load))
events.sort(key=lambda x: x[0])
current_load = 0
max_load = 0
for _, load_change in events:
current_load += load_change
max_load = max(max_load, current_load)
return max_load
jobs = [[1,4,3], [2,5,4], [7,9,6]]
print(find_max_cpu_load(jobs))Solution
Step 1: List all events sorted by time:
Events: (1,+3), (2,+4), (4,-3), (5,-4), (7,+6), (9,-6)Step 2: Trace current_load and max_load through events:
At 1: current_load=3, max_load=3; at 2: current_load=7, max_load=7; at 4: current_load=4, max_load=7; at 5: current_load=0, max_load=7; at 7: current_load=6, max_load=7; at 9: current_load=0, max_load=7.Final Answer:
Option A -> Option AQuick Check:
Maximum overlap load is 7 at time 2 [OK]
- Off-by-one in event processing
- Ignoring negative load at job end
words = ["apple", "banana", "apple", "orange", "banana", "banana"] and k = 2, what is the returned list?Solution
Step 1: Count frequencies
apple: 2, banana: 3, orange: 1; max_freq = 3Step 2: Fill buckets and sort within buckets
buckets[3] = ["banana"], buckets[2] = ["apple"], buckets[1] = ["orange"]; iterate from freq=3 downFinal Answer:
Option A -> Option AQuick Check:
Top 2 frequent words are banana (3), apple (2) [OK]
- Confusing lex order with frequency order
- Off-by-one in bucket indices
- Returning words in ascending frequency order
Solution
Step 1: Analyze sorting and heap initialization
Sorting takes O(n log n). Initial heap push for n-1 pairs is O(n log n) but dominated by sorting.Step 2: Analyze k iterations of heap operations
Each pop and push operation costs O(log n), repeated k times -> O(k log n).Final Answer:
Option A -> Option AQuick Check:
Heap size bounded by n, so log n factor dominates [OK]
- Confusing k log k with k log n
- Assuming O(n^2 log n) due to pairs
- Ignoring heap size limits
Solution
Step 1: Check bucket list size
Buckets are created with size max_freq, but frequencies range from 1 to max_freq inclusive, so index max_freq is needed.Step 2: Consequence of incorrect bucket size
Accessing buckets[count] where count == max_freq causes IndexError or missing elements if bucket list is too small.Final Answer:
Option C -> Option CQuick Check:
Bucket list must have max_freq + 1 slots to index frequencies correctly [OK]
- Off-by-one bucket size
- Misindexing buckets causing runtime errors
