Skip to content

Commit 69cd08f

Browse files
author
Brian Elinsky
committed
Log reverse_sublist attempt (solved with hints, optimal) and add flashcards
1 parent c51e246 commit 69cd08f

File tree

11 files changed

+86
-11
lines changed

11 files changed

+86
-11
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ My journey through Elements of Programming Interviews in Python.
88
Problems attempted but not yet solved
99

1010
☑️☑️☑️☑️☑️☑️☑️☑️☑️☑️☑️☑️☑️☑️☑️☑️☑️☑️☑️☑️
11-
☑️☑️☑️☑️⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
11+
☑️☑️☑️☑️☑️⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
1212
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
1313
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
1414
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
@@ -19,13 +19,13 @@ Problems attempted but not yet solved
1919
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
2020
⬜⬜⬜⬜
2121

22-
**24 / 204** (11.8%)
22+
**25 / 204** (12.3%)
2323

2424
### Tier 1: Solved 👍
2525
Problems solved (with or without help)
2626

2727
👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍
28-
👍⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
28+
👍👍⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
2929
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
3030
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
3131
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
@@ -36,7 +36,7 @@ Problems solved (with or without help)
3636
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
3737
⬜⬜⬜⬜
3838

39-
**21 / 204** (10.3%)
39+
**22 / 204** (10.8%)
4040

4141
### Tier 2: Solved Independently 💪
4242
Problems solved without hints or looking at solutions
@@ -88,12 +88,12 @@ Progress toward priority-based learning goals.
8888

8989
**6 / 16** problems mastered
9090

91-
### 🟠 P1: Attempt All (5/21 - 24%)
91+
### 🟠 P1: Attempt All (6/21 - 29%)
9292

93-
☑️☑️☑️☑️☑️⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
93+
☑️☑️☑️☑️☑️☑️⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
9494
9595

96-
**5 / 21** problems attempted
96+
**6 / 21** problems attempted
9797

9898
### 🟠 P1: Master All (1/21 - 5%)
9999

@@ -284,7 +284,7 @@ flowchart TB
284284
| 4: Primitive Types | 11 | 5 (45%) | 5 (45%) | 3 (27%) | 2 (18%) |
285285
| 5: Arrays | 20 | 4 (20%) | 3 (15%) | 2 (10%) | 2 (10%) |
286286
| 6: Strings | 12 | 3 (25%) | 3 (25%) | 1 (8%) | 1 (8%) |
287-
| 7: Linked Lists | 13 | 1 (8%) | 1 (8%) | 1 (8%) | 1 (8%) |
287+
| 7: Linked Lists | 13 | 2 (15%) | 2 (15%) | 1 (8%) | 1 (8%) |
288288
| 8: Stacks and Queues | 9 | 1 (11%) | 1 (11%) | 1 (11%) | 0 (0%) |
289289
| 9: Binary Trees | 15 | 1 (7%) | 0 (0%) | 0 (0%) | 0 (0%) |
290290
| 10: Heaps | 6 | 1 (17%) | 1 (17%) | 0 (0%) | 0 (0%) |
@@ -297,7 +297,7 @@ flowchart TB
297297
| 17: Greedy Algorithms and Invariants | 8 | 1 (12%) | 1 (12%) | 0 (0%) | 0 (0%) |
298298
| 18: Graphs | 8 | 1 (12%) | 1 (12%) | 0 (0%) | 0 (0%) |
299299
| 24: Honors Class | 35 | 0 (0%) | 0 (0%) | 0 (0%) | 0 (0%) |
300-
| **Total** | **204** | **24** | **21** | **11** | **8** |
300+
| **Total** | **204** | **25** | **22** | **11** | **8** |
301301

302302
## All Problems
303303

@@ -370,7 +370,7 @@ flowchart TB
370370
| # | Problem | Priority | Status | Best Time |
371371
|---|---------|----------|--------|-----------|
372372
| 7.01 | Merge two sorted lists | 🔴 | 🏆 | 3 min |
373-
| 7.02 | Reverse a single sublist | 🟠 | | |
373+
| 7.02 | Reverse a single sublist | 🟠 | 👍 | |
374374
| 7.03 | Test for cyclicity | 🟠 | | |
375375
| 7.04 | Test for overlapping lists---lists are cycle-free | 🟡 | | |
376376
| 7.05 | Test for overlapping lists---lists may have cycles | | | |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Why Dummy Node
2+
3+
**Q:** In reverse_sublist, why use a dummy node?
4+
5+
**A:** Handles the case where `start=1`. Without it, we'd have no node before the sublist to anchor to.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Two Pointers for Reversal
2+
3+
**Q:** In reverse_sublist, what two pointers do you need for the reversal, and where do they point?
4+
5+
**A:**
6+
- `sublist_head`: node BEFORE the sublist (stays fixed)
7+
- `sublist_iter`: first node OF the sublist (stays on same node, ends up last)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Positioning Before Sublist
2+
3+
**Q:** To position `sublist_head` at the node before position `start` (1-indexed), how many times do you advance from the dummy node?
4+
5+
**A:** `start - 1` times. If `start=1`, advance 0 times (stay at dummy). If `start=2`, advance 1 time.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Iteration Count
2+
3+
**Q:** To reverse a sublist from position `start` to `finish`, how many iterations of the reversal loop?
4+
5+
**A:** `finish - start` iterations. A sublist of length 3 needs 2 moves to reverse.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Reversal Concept
2+
3+
**Q:** In reverse_sublist, what does each iteration of the reversal loop do conceptually?
4+
5+
**A:** Takes the node after `sublist_iter` and moves it to the front of the sublist (right after `sublist_head`).
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Visual Trace
2+
3+
**Q:** Show the list after each iteration of reversing `1 → [2 → 3 → 4 → 5] → 6` (positions 2-5).
4+
5+
**A:**
6+
- Initial: `1 → [2 → 3 → 4 → 5] → 6`
7+
- After iteration 1 (move 3 to front): `1 → [3 → 2 → 4 → 5] → 6`
8+
- After iteration 2 (move 4 to front): `1 → [4 → 3 → 2 → 5] → 6`
9+
- After iteration 3 (move 5 to front): `1 → [5 → 4 → 3 → 2] → 6`
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Move Node to Front (Concept)
2+
3+
**Q:** What are the steps to move a node to the front of the sublist (no code)?
4+
5+
**A:**
6+
1. Save a pointer to the node to move (the one after `sublist_iter`)
7+
2. Cut it out of the chain
8+
3. Splice it in at the front (right after `sublist_head`)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Move Node to Front (Code)
2+
3+
**Q:** What are the 4 pointer operations to move `sublist_iter.next` to the front of the sublist?
4+
5+
**A:**
6+
```python
7+
node_to_move = sublist_iter.next # 1. save pointer to node to move
8+
sublist_iter.next = node_to_move.next # 2. cut it out of chain
9+
node_to_move.next = sublist_head.next # 3. splice: point to current front
10+
sublist_head.next = node_to_move # 4. splice: becomes new front
11+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Full Algorithm
2+
3+
**Q:** Describe the full algorithm for reverse_sublist(L, start, finish).
4+
5+
**A:**
6+
1. Create dummy node pointing to L
7+
2. Advance `start - 1` times to position `sublist_head` (node before sublist)
8+
3. Set `sublist_iter = sublist_head.next` (first node of sublist)
9+
4. Loop `finish - start` times:
10+
- Save pointer to node after `sublist_iter`
11+
- Cut it out of chain
12+
- Splice it in at front (after `sublist_head`)
13+
5. Return `dummy.next`

0 commit comments

Comments
 (0)