Practice
pop() call?
fs = FreqStack()
fs.push(1)
fs.push(2)
fs.push(1)
print(fs.pop())
print(fs.pop())
Solution
Step 1: Trace push operations
Push 1: freq[1]=1, time=1, heap push (-1, -1, 1) Push 2: freq[2]=1, time=2, heap push (-1, -2, 2) Push 1: freq[1]=2, time=3, heap push (-2, -3, 1)Step 2: Trace pop operations
First pop: heap pops (-2, -3, 1) -> returns 1, freq[1]=1 Second pop: heap now has (-1, -2, 2) and (-1, -1, 1). Max freq is 1, tie broken by time: -2 > -1, so pop (-1, -2, 2) returns 2.Final Answer:
Option C -> Option CQuick Check:
Most frequent element 1 popped first, then next most recent with freq 1 is 2 [OK]
- Ignoring timestamp order in heap
- Mixing up pop order when frequencies tie
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]
- Sorting entire stream each time is too slow
- Using max-heap of all elements wastes space and time
- Dynamic programming is irrelevant here
Solution
Step 1: Understand the problem constraints
Computing XOR values for each coordinate independently with nested loops leads to O(m^2 * n^2) time, which is inefficient.Step 2: Recognize prefix XOR and min-heap usage
Using a prefix XOR matrix allows O(m * n) computation of all XOR coordinate values. Maintaining a min-heap of size k ensures we track the kth largest value efficiently, leading to O(m * n * log k) time.Final Answer:
Option D -> Option DQuick Check:
Prefix XOR + min-heap is optimal for kth largest XOR coordinate value [OK]
- Thinking nested loops with sorting is efficient enough
- Assuming greedy selection works for XOR
- Confusing DP with prefix XOR usage
Solution
Step 1: Understand frequency distribution
Counting frequencies is straightforward, but sorting all words fully costs O(n log n).Step 2: Use bucket sort to group words by frequency
Bucket sort groups words by frequency in O(n), then sorting lex order within buckets is efficient since buckets are smaller.Final Answer:
Option D -> Option DQuick Check:
Bucket sort + lex sort within buckets achieves O(n) time [OK]
- Assuming full sort is optimal
- Using DP for frequency counting
- Greedy without sorting fails tie-break
Solution
Step 1: Identify binary search iterations
Binary search on value range performs O(log(max - min)) iterations.Step 2: Analyze countLessEqual complexity
Counting elements ≤ mid takes O(n) time by traversing from bottom-left to top-right.Step 3: Combine complexities
Total time is O(n * log(max - min)). However, since n is matrix dimension, counting is O(n), but matrix size is n², so counting is O(n) per iteration, total O(n log(max-min)).Final Answer:
Option C -> Option CQuick Check:
Counting is O(n), binary search O(log(max-min)) [OK]
- Confusing n with n² in counting
- Assuming sorting complexity applies
