Practice
browserHistory = BrowserHistory("leetcode.com")
browserHistory.visit("google.com")
browserHistory.visit("facebook.com")
browserHistory.visit("youtube.com")
print(browserHistory.back(1))
print(browserHistory.back(1))
print(browserHistory.forward(1))
Solution
Step 1: Trace back(1) after visiting youtube.com
back_stack = ["leetcode.com", "google.com", "facebook.com", "youtube.com"] Pop "youtube.com" to forward_stack, back_stack top is "facebook.com".Step 2: Trace back(1) again and forward(1)
Second back(1): pop "facebook.com" to forward_stack, top is "google.com". Forward(1): pop "facebook.com" from forward_stack back to back_stack, top is "facebook.com".Final Answer:
Option C -> Option CQuick Check:
Last forward returns "facebook.com" as expected [OK]
- Off-by-one errors in popping stacks
- Confusing forward and back stacks
- Returning wrong top element after operations
Solution
Step 1: Identify operation steps
addAtTail uses the tail pointer to append a new node directly without traversal.Step 2: Analyze complexity
Since tail pointer gives direct access, adding at tail is O(1). Size update is O(1) as it's a simple increment.Final Answer:
Option C -> Option CQuick Check:
Tail pointer eliminates traversal, so addAtTail is constant time [OK]
- Assuming traversal needed to find tail
- Confusing size update cost
- Thinking sentinel nodes balance list
Solution
Step 1: Understand tail update condition
The tail pointer should update when deleting the last node, which is at index size-1 before deletion.Step 2: Identify bug
At line 8, the tail pointer is set to prev, but prev may not be the correct new tail if the deleted node was the last node. The tail pointer should be updated after size decrement and possibly set to the correct node.Step 3: Correct fix
Update tail pointer after size decrement and ensure it points to the last node in the list.Final Answer:
Option A -> Option AQuick Check:
Tail pointer update must correctly reflect the new last node after deletion [OK]
- Updating tail before size decrement
- Not updating tail on last node deletion
- Incorrect traversal to prev node
Solution
Step 1: Identify complexity of outer and inner loops
The algorithm traverses the list once, performing constant-time pointer operations per node, so time is O(n).Step 2: Check if recursion stack adds extra space
No recursion or extra data structures are used; only a few pointers are maintained, so space is O(1).Final Answer:
Option A -> Option AQuick Check:
Single pass with constant pointers -> O(n) time and O(1) space [OK]
- Confusing with sorting complexity O(n log n)
- Assuming extra arrays cause O(n) space
- Mistaking pointer updates as nested loops causing O(n^2)
n into k parts, and why?Solution
Step 1: Identify passes over the list
One pass to count total nodes (O(n)), one pass to split nodes into k parts (O(n)).Step 2: Account for overhead
Creating dummy heads and managing k parts adds O(k) overhead, but does not dominate.Final Answer:
Option A -> Option AQuick Check:
Two passes over n plus O(k) overhead is O(n), since k ≤ n [OK]
- Assuming nested loops cause O(n*k)
- Ignoring overhead of dummy heads
- Confusing single pass with O(n) ignoring k
