Practice
Solution
Step 1: Understand the problem requirements
The problem requires detecting duplicates efficiently in an array.Step 2: Compare approaches based on time complexity
Sorting then scanning is O(n log n), nested loops is O(n²), and dynamic programming is unnecessary here. Using a hash set allows O(n) time with O(n) space by checking membership during one pass.Final Answer:
Option B -> Option BQuick Check:
Hash set membership check is O(1) average, enabling linear time detection [OK]
- Confusing sorting approach as optimal
- Thinking nested loops are efficient enough
- Misapplying DP to a membership problem
Solution
Step 1: Understand operation requirements
Insert, delete, and getRandom must all be O(1) average time.Step 2: Evaluate data structures
Balanced BST or sorted array have O(log n) or worse for insert/delete. Linked list lacks O(1) random access. Hash map + array with swap-delete achieves O(1) for all operations.Final Answer:
Option B -> Option BQuick Check:
Hash map + array with swap-delete is the classic O(1) solution [OK]
- Thinking balanced BST gives O(1) insert/delete
- Using linked list for O(1) random access
- Assuming sorted array can do O(1) insert/delete
def isIsomorphic(s: str, t: str) -> bool:
if len(s) != len(t):
return False
s_to_t = {}
t_to_s = {}
for c1, c2 in zip(s, t):
if c1 in s_to_t:
if s_to_t[c1] != c2:
return False
else:
s_to_t[c1] = c2
if c2 in t_to_s:
if t_to_s[c2] != c1:
return False
else:
t_to_s[c2] = c1
return True
print(isIsomorphic("egg", "add"))
What is the output of this code?Solution
Step 1: Trace first iteration with c1='e', c2='a'
Both maps are empty, so s_to_t['e'] = 'a' and t_to_s['a'] = 'e'.Step 2: Trace second and third iterations
Second iteration: c1='g', c2='d', maps updated similarly. Third iteration: c1='g' already maps to 'd', and c2='d' maps back to 'g', so no conflict.Final Answer:
Option A -> Option AQuick Check:
Mapping is consistent for all characters, so returns True [OK]
- Confusing output with 'foo' and 'bar'
- Assuming length mismatch causes exception
get method for the time-based key-value store. Which line contains the subtle bug that causes incorrect results when querying timestamps less than the smallest stored timestamp?Solution
Step 1: Understand difference between bisect_left and bisect_right
bisect_left returns insertion index to the left, which can cause off-by-one errors when timestamp equals stored timestamps.Step 2: Identify impact on retrieval
Using bisect_left causes incorrect index i for timestamps equal to stored ones, leading to wrong value or empty string returned.Final Answer:
Option C -> Option CQuick Check:
bisect_right is required to get correct insertion point for retrieval [OK]
- Using bisect_left instead of bisect_right
- Incorrect boundary check for i==0
- Returning wrong index value
get should return the most recently set value at or before the timestamp. Which modification to the data structure and retrieval algorithm is correct?Solution
Step 1: Understand requirement for multiple values per timestamp
Multiple values at same timestamp require storing duplicates and retrieving the most recent one.Step 2: Analyze retrieval with duplicates
Using bisect_right on timestamps list with duplicates returns insertion index after all duplicates; returning value at i-1 gives the last inserted value at that timestamp.Step 3: Confirm data structure modification
Storing timestamps with duplicates in order and values aligned allows correct retrieval without linear search or complex structures.Final Answer:
Option B -> Option BQuick Check:
bisect_right with duplicates returns correct index for last inserted value [OK]
- Using linear search causing inefficiency
- Using stacks requiring pop on get
- Storing multiple values in nested lists complicates retrieval
