Practice
cs = CustomStack(3)
cs.push(1)
cs.push(2)
cs.increment(2, 5)
print(cs.pop())
Solution
Step 1: Trace the increment operation
After pushing 1 and 2, stack = [1, 2], inc = [0, 0, 0]. increment(2, 5) sets inc[1] += 5 -> inc = [0, 5, 0].Step 2: Trace the pop operation
pop() removes top element at index 1 (value 2). Since i=1 > 0, inc[0] += inc[1] -> inc[0] = 0 + 5 = 5. Result = stack.pop() + inc[1] = 2 + 5 = 7. inc[1] reset to 0.Final Answer:
Option B -> Option BQuick Check:
Increment applied lazily, pop returns 7 [OK]
- Forgetting to propagate increments down on pop
- Adding increments directly to popped element only
Solution
Step 1: Understand the operations required
Insert, remove (one occurrence), and getRandom must all be average O(1), even with duplicates.Step 2: Evaluate approaches
A simple list with linear search (A) leads to O(n) removals. Balanced BST (C) has O(log n) operations. Queue and hash set (D) do not support random access efficiently. Hash map + list with index tracking (B) supports O(1) insert, remove, and getRandom by maintaining indices of duplicates.Final Answer:
Option C -> Option CQuick Check:
Hash map + list with index tracking enables O(1) average operations [OK]
- Assuming balanced BST can do O(1) getRandom
- Using linear search for removal
- Ignoring duplicates in data structure
class CustomStack:
def __init__(self, maxSize: int):
self.stack = []
self.inc = [0] * maxSize
self.maxSize = maxSize
def push(self, x: int) -> None:
self.stack.append(x) # Line 8
def pop(self) -> int:
if not self.stack:
return -1
i = len(self.stack) - 1
if i > 0:
self.inc[i-1] += self.inc[i]
res = self.stack.pop() + self.inc[i]
self.inc[i] = 0
return res
def increment(self, k: int, val: int) -> None:
limit = min(k, len(self.stack))
if limit > 0:
self.inc[limit-1] += val
Solution
Step 1: Check push method
Line 8 appends element without checking if stack size is less than maxSize, violating constraints and allowing overflow.Step 2: Verify other lines
Returning -1 on empty pop is correct; increment propagation and inc array updates are correct.Final Answer:
Option A -> Option AQuick Check:
Push must check maxSize to avoid overflow [OK]
- Forgetting maxSize check in push
- Incorrect increment propagation on pop
put operation in the optimal LFU Cache implementation using a frequency map of doubly linked lists and hash maps?Solution
Step 1: Analyze data structures used
Hash maps provide O(1) access to keys and frequencies. Doubly linked lists allow O(1) insertion and deletion of keys within frequency groups.Step 2: Consider operations in put
Eviction and frequency updates involve O(1) operations on hash maps and linked lists. Frequency increments and min frequency updates are also O(1).Final Answer:
Option D -> Option DQuick Check:
Optimal LFU cache achieves O(1) amortized put [OK]
- Assuming O(n) due to scanning keys
- Thinking frequency updates cost O(log n)
- Confusing distinct frequencies with n
import bisect
class SnapshotArray:
def __init__(self, length: int):
self.snap_id = 0
self.data = [[(-1, 0)] for _ in range(length)]
def set(self, index: int, val: int) -> None:
# Bug here
self.data[index].append((self.snap_id, val))
def snap(self) -> int:
self.snap_id += 1
return self.snap_id - 1
def get(self, index: int, snap_id: int) -> int:
arr = self.data[index]
i = bisect.bisect_right(arr, (snap_id, float('inf'))) - 1
return arr[i][1]
Solution
Step 1: Analyze set() method
The set method always appends (snap_id, val) without checking if the last entry has the same snap_id, causing duplicate entries for the same snap_id.Step 2: Consequence on get()
Duplicate entries for the same snap_id break binary search assumptions and can cause get to return incorrect values.Final Answer:
Option C -> Option CQuick Check:
Correct code replaces last entry if snap_id matches [OK]
- Appending blindly causes duplicates
- Misunderstanding initialization defaults
