Filter or compact an array in place with a read pointer and a write pointer.
Use it when you must remove or compact values in place and keep the order of the rest. Signal words: in place, remove, compact, deduplicate, O(1) extra memory, keep the order, without a second array.
Start read and write both at 0.
Step read visits every value. If you keep it, copy it to slot write and move write on. If not, only read moves.
Stop when read reaches the end. Return the kept prefix, values[0..write−1].
Keep at most m copies of each value in sorted data: compare the value at read with values[write − m], in the kept prefix.
It needs an output no longer than the input, so write never passes read.
Cost: each value is read once and copied at most once, so O(n) time and O(1) extra space.
In this lesson you learn to remove values from an array in place, in one pass, without a second array. Two pointers move in the same direction: one reads every value, and one writes only the values you keep.
Every loading dock at a delivery hub has a gate scanner. It reads the label of each parcel that goes onto a truck. Your team lead sets the requirement for the scanner's cleanup step.
Your team lead says: When the buffer is full, the gate removes the no-reads and uploads the other scans, in scan order.
The gate cannot scan while it cleans up, so the cleanup must take under one second.
A no-read is a scan where the label could not be read. Parcel codes are positive, so the gate stores a no-read as -1. The gate's controller has a fixed buffer and no memory for a second one.
Here is one short test buffer: [52, -1, 37, -1, -1, 18, 64]. The gate should upload [52, 37, 18, 64].
So the task is this: remove every -1 inside the same buffer, and keep the other scans in their order.
Picture a to-do list written on numbered lines, with some items crossed out. You want a clean list on the same sheet, with no new paper. You put one finger on the first line and the tip of your pencil on the first line too.
Your finger moves down one line at a time and reads each item. A crossed-out item: the finger moves on, and the pencil stays. An item you keep: you write it on the pencil's line, and the pencil moves down one line.
Your finger is the read pointer: it visits every item once. Your pencil is the write pointer: it moves only when you keep an item. The lines above the pencil hold the clean list. The pencil is never below your finger, so you never write over a line you have not read.
Cleaning a list on one sheet: your finger reads every line, and a pencil writes the kept lines at the top.
Before you write code, clean the test buffer by hand. It takes two minutes, and it shows you where the work goes.
Solve it by hand: The dock gate buffer by hand: remove the no-reads. The hand-solve sheet loads in your browser.
You work through it on the whiteboard. You find each no-read from the left, delete it, and slides every later scan one slot left.
Input: [52, -1, 37, -1, -1, 18, 64].
| Step | No-read deleted at index | Scans that slide left | Values moved | Buffer after |
|---|---|---|---|---|
| 1 | 1 | 37, -1, -1, 18, 64 | 5 | 52, 37, -1, -1, 18, 64 |
| 2 | 2 | -1, 18, 64 | 3 | 52, 37, -1, 18, 64 |
| 3 | 2 | 18, 64 | 2 | 52, 37, 18, 64 |
The clean buffer is [52, 37, 18, 64]. You moved 10 values to get it.
Look at 18 and 64. Each one moved three times, one slot at a time, and both were kept all along.
Remember this. You will use it soon.
You turn the hand method into code.
Start with a correct version. Delete each no-read and slide the later scans left.
def remove_no_reads(scans):
n = len(scans)
i = 0
while i < n:
if scans[i] == -1:
for j in range(i, n - 1): # slide every later scan one slot left
scans[j] = scans[j + 1]
n -= 1 # the buffer now ends one slot earlier
else:
i += 1 # move on only when the scan stays
return scans[:n]This is correct. Look at the last line of the loop: after a deletion, i stays where it is, because the next scan has slid into slot i. For the test buffer it moves 10 values, which takes no time at all. The team keeps it as the reference: every faster version must give the same answers.
How much work is it in general? Each deletion moves every scan after it. With many no-reads, that is about n moves for each of them, so the cost grows like n^2.
Now check the real gates.
A busy dock gate fills a buffer of 100,000 scans. On wet days, one scan in five is a no-read.
Here is the count for one full buffer:
The team also counts the moves on a seeded test buffer of 100,000 scans with 20,066 no-reads. The first attempt moves 1,003,367,271 values, which matches the estimate.
Predict the cost
Q A computer does about 10^8 simple operations a second. About how long does the first attempt take to clean one full buffer?
Answer: C
10^9 ÷ 10^8 = 10 seconds. The gate cannot scan during the cleanup, and the limit is 1 second, so the first attempt is about 10 times too slow.
The first attempt is correct, but at this size it cannot keep up. Now the team looks for the work it repeats.
Look at the scans that slide again and again.
Every shift moves scans that the pass keeps anyway, one slot at a time. Can we do better?
Think about where 18 ends up. In the clean buffer it sits in slot 2, because two kept scans come before it: 52 and 37. Every kept scan's final slot is the number of kept scans before it.
So do not slide anything. Walk the buffer once, and move each kept scan straight to its final slot.
You need two indexes for this:
When the scan at read is kept, copy it to the slot at write, and move write one step on. When it is a no-read, only read moves on.
These two indexes are called read and write pointers. They are two pointers, like left and right in the last lesson, but both move in the same direction. Sliding windows, in Module 5, build on same-direction pointers too.
The slots before write hold every scan kept so far, in order. That front part of the array is the kept prefix. When read reaches the end, the kept prefix is the answer.
| Read | Value | Kept? | Copied to slot | Write after | Kept prefix |
|---|---|---|---|---|---|
| 0 | 52 | yes | 0, its own slot | 1 | 52 |
| 1 | -1 | no | none | 1 | 52 |
| 2 | 37 | yes | 1 | 2 | 52, 37 |
| 3 | -1 | no | none | 2 | 52, 37 |
| 4 | -1 | no | none | 2 | 52, 37 |
| 5 | 18 | yes | 2 | 3 | 52, 37, 18 |
| 6 | 64 | yes | 3 | 4 | 52, 37, 18, 64 |
The answer is the same as your: [52, 37, 18, 64]. This time 18 moved once, straight to slot 2. The whole pass made 4 copies instead of 10 moves.
Read visits every slot, write marks the next free one: each kept scan is copied once, straight to its final place.
Write moves only in a step where read also moves, and at most once in that step. Both start at 0. So write is always at read or behind it.
That matters for safety. The slot at write has already been read, so the copy never destroys a scan you still need. When write equals read, the scan is copied onto itself, as 52 was.
Race: Delete and shift against read and write pointers. The operation counter loads in your browser.
The race counts the values each version moves, on buffers where one scan in five is a no-read. At 10,000 scans, the first attempt moves 9,693,292 values. Read and write copy 8,092. For a full buffer of 100,000 scans, read and write need 100,000 reads and about 80,000 copies, instead of about 10^9 moves.
Write moves only when read moves, so write is never ahead of read and never overwrites a value not yet read.
A second gate has a buffer of [-1, 64, -1, 27, 27, -1, 85]. The player stops at five steps and asks you to predict.
Dry run: Predict each step: a second gate's buffer. The step-by-step player loads in your browser.
Reading without the player? Cover the last three columns and predict them row by row.
| Read | Value | Kept? | Copied to slot | Write after |
|---|---|---|---|---|
| 0 | -1 | no | none | 0 |
| 1 | 64 | yes | 0 | 1 |
| 2 | -1 | no | none | 1 |
| 3 | 27 | yes | 1 | 2 |
| 4 | 27 | yes | 2 | 3 |
| 5 | -1 | no | none | 3 |
| 6 | 85 | yes | 3 | 4 |
The first scan is a no-read, so write falls behind read at once. The two copies of 27 are both kept, because only -1 is removed. The gate uploads 4 scans: [64, 27, 27, 85].
Here is the rule that keeps the pass correct. Every line of the templates below exists to protect it.
values[0..write−1] holds exactly the kept values among values[0..read−1], in their original order, and write never passes read.
At the start, read and write are both 0, and both ranges are empty, so the rule holds.
Now read takes one step. If the value is kept, it is copied to slot write, and write moves on: the kept prefix gains that value, in order. If it is not kept, the kept prefix does not change, and the rule still holds.
When read reaches the end, values[0..read−1] is the whole array. So the kept prefix holds every kept value, in order, and nothing else.
Use it when you must remove or compact values in place and keep the order of the rest. Signal words: in place, remove, compact, deduplicate, O(1) extra memory, keep the order, without a second array.
Start: read and write both at 0.
Step: read visits every value. If you keep it, copy it to slot write and move write on. If not, only read moves.
Stop: when read reaches the end. Return the kept prefix, values[0..write−1].
Keep at most m copies of each value in sorted data: compare the value at read with values[write − m], in the kept prefix.
It needs an output no longer than the input, so write never passes read.
Cost: each value is read once and copied at most once, so O(n) time and O(1) extra space.
def remove_value(values, unwanted):
# [1] Nothing is kept yet, so write starts at the front.
write = 0
# [2] Read visits every value once, from left to right.
for read in range(len(values)):
# [3] Keep the value only if it is not the unwanted one.
if values[read] != unwanted:
# [4] Copy it to the write slot and advance write; write never passes read.
values[write] = values[read]
write += 1
# [5] The kept prefix, values[0..write-1], is the answer.
return values[:write]public int[] removeValue(int[] values, int unwanted) {
// [1] Nothing is kept yet, so write starts at the front.
int write = 0;
// [2] Read visits every value once, from left to right.
for (int read = 0; read < values.length; read++) {
// [3] Keep the value only if it is not the unwanted one.
if (values[read] != unwanted) {
// [4] Copy it to the write slot and advance write; write never passes read.
values[write] = values[read];
write++;
}
}
// [5] The kept prefix, values[0..write-1], is the answer.
return Arrays.copyOf(values, write);
}vector<int> removeValue(vector<int>& values, int unwanted) {
// [1] Nothing is kept yet, so write starts at the front.
int write = 0;
// [2] Read visits every value once, from left to right.
for (int read = 0; read < (int) values.size(); read++) {
// [3] Keep the value only if it is not the unwanted one.
if (values[read] != unwanted) {
// [4] Copy it to the write slot and advance write; write never passes read.
values[write] = values[read];
write++;
}
}
// [5] The kept prefix, values[0..write-1], is the answer.
return vector<int>(values.begin(), values.begin() + write);
}function removeValue(values, unwanted) {
// [1] Nothing is kept yet, so write starts at the front.
let write = 0;
// [2] Read visits every value once, from left to right.
for (let read = 0; read < values.length; read++) {
// [3] Keep the value only if it is not the unwanted one.
if (values[read] !== unwanted) {
// [4] Copy it to the write slot and advance write; write never passes read.
values[write] = values[read];
write++;
}
}
// [5] The kept prefix, values[0..write-1], is the answer.
return values.slice(0, write);
}Rebuild the template: put its lines back in order. Loads in your browser.
The test in step [3] is the only line that knows what "kept" means. Change that one line, and the same loop removes zeros, drops values below a limit or keeps only even codes.
Sometimes a value may stay, but only a few times. Say the values are sorted, so equal values sit next to each other, and each value may appear at most m times.
Look at the kept prefix, m slots back from write: values[write − m]. If it equals the value at read, then the last m kept values are all copies of it, because sorted values cannot change and come back. So skip it; otherwise keep it.
While write is less than m, fewer than m values are kept, so you keep the value without comparing.
def dedupe_sorted(values, m):
# [1] Nothing is kept yet, so write starts at the front.
write = 0
# [2] Read visits every value once, from left to right.
for read in range(len(values)):
# [3] Keep the value unless the kept prefix already ends with m copies of it: compare m slots back from write.
if write < m or values[read] != values[write - m]:
# [4] Copy it to the write slot and advance write; write never passes read.
values[write] = values[read]
write += 1
# [5] The kept prefix, values[0..write-1], is the answer.
return values[:write]public int[] dedupeSorted(int[] values, int m) {
// [1] Nothing is kept yet, so write starts at the front.
int write = 0;
// [2] Read visits every value once, from left to right.
for (int read = 0; read < values.length; read++) {
// [3] Keep the value unless the kept prefix already ends with m copies of it: compare m slots back from write.
if (write < m || values[read] != values[write - m]) {
// [4] Copy it to the write slot and advance write; write never passes read.
values[write] = values[read];
write++;
}
}
// [5] The kept prefix, values[0..write-1], is the answer.
return Arrays.copyOf(values, write);
}vector<int> dedupeSorted(vector<int>& values, int m) {
// [1] Nothing is kept yet, so write starts at the front.
int write = 0;
// [2] Read visits every value once, from left to right.
for (int read = 0; read < (int) values.size(); read++) {
// [3] Keep the value unless the kept prefix already ends with m copies of it: compare m slots back from write.
if (write < m || values[read] != values[write - m]) {
// [4] Copy it to the write slot and advance write; write never passes read.
values[write] = values[read];
write++;
}
}
// [5] The kept prefix, values[0..write-1], is the answer.
return vector<int>(values.begin(), values.begin() + write);
}function dedupeSorted(values, m) {
// [1] Nothing is kept yet, so write starts at the front.
let write = 0;
// [2] Read visits every value once, from left to right.
for (let read = 0; read < values.length; read++) {
// [3] Keep the value unless the kept prefix already ends with m copies of it: compare m slots back from write.
if (write < m || values[read] !== values[write - m]) {
// [4] Copy it to the write slot and advance write; write never passes read.
values[write] = values[read];
write++;
}
}
// [5] The kept prefix, values[0..write-1], is the answer.
return values.slice(0, write);
}Keeping at most m copies in sorted data: compare with the kept value m slots back from write, and skip if it matches.
Exercise · Predict the output: What is left in the array?. Sorted alert ids, at most 2 per account. The code prints the number kept, a bar, then the whole array after the pass. What does it print?
values = [5, 5, 5, 7, 7, 7, 7, 9]
m = 2
write = 0
for read in range(len(values)):
if write < m or values[read] != values[write - m]:
values[write] = values[read]
write += 1
print(write, "|", *values)A bank app sends an alert when an account's balance drops below its owner's limit. One batch lists the account id of each alert, sorted: [1042, 1042, 1042, 1187, 2210, 2210, 2210, 2210, 3005]. The policy is at most 2 alerts per account in one batch.
This is Template 2 with m = 2.
| Read | Value | Write | values[write − 2] | Kept? | Kept prefix after |
|---|---|---|---|---|---|
| 0 | 1042 | 0 | none, write < 2 | yes | 1042 |
| 1 | 1042 | 1 | none, write < 2 | yes | 1042, 1042 |
| 2 | 1042 | 2 | 1042 | no | 1042, 1042 |
| 3 | 1187 | 2 | 1042 | yes | 1042, 1042, 1187 |
| 4 | 2210 | 3 | 1042 | yes | 1042, 1042, 1187, 2210 |
| 5 | 2210 | 4 | 1187 | yes | 1042, 1042, 1187, 2210, 2210 |
| 6 | 2210 | 5 | 2210 | no | 1042, 1042, 1187, 2210, 2210 |
| 7 | 2210 | 5 | 2210 | no | 1042, 1042, 1187, 2210, 2210 |
| 8 | 3005 | 5 | 2210 | yes | 1042, 1042, 1187, 2210, 2210, 3005 |
The batch sends 6 alerts: [1042, 1042, 1187, 2210, 2210, 3005]. Look at read = 5: the template compares with slot 3 of the kept prefix, which is 1187, so the second 2210 is kept. You will see in Stretch what goes wrong when the comparison looks somewhere else.
Exercise · Find the bug: One copy too many. This function keeps at most m copies of each value in a sorted list. For some inputs it keeps m + 1. Which line is wrong?
def dedupe_sorted(values, m):
write = 0
for read in range(len(values)):
if write <= m or values[read] != values[write - m]:
values[write] = values[read]
write += 1
return values[:write]Real production data brings edge cases.
On a dry day the buffer may hold no no-reads at all: [52, 37, 18]. Every scan is kept, so write stays equal to read, and each scan is copied onto itself.
The template returns [52, 37, 18], the buffer unchanged. No special case is needed.
A gate with a dirty lens reads nothing: [-1, -1, -1]. Read visits all three slots, but no scan is kept, so write stays at 0.
The kept prefix is empty, so the template returns []. Agree with the caller that an empty upload is valid and is not an error.
The buffer [-1] returns [], and [52] returns [52]. An empty buffer [] returns []: the loop never runs. Test all three, so the code never reads past either end.
Run Template 2 on [4, 4, 9, 9, 9] with m = 5. No value appears 5 times, so every value is kept, and the answer is the input unchanged.
Here the guard write < m does the work. Without it, write − m is negative. In Python, values[-5] reads from the far end of the list, and this input returns [9, 9]. In Java and C++, the same index is outside the array.
Exercise · Break it: Find the input this clean-up gets wrong. This function removes every copy of the unwanted value in place, but it starts read and write at 1. Find an input where it gives the wrong result.
def remove_value(values, unwanted):
if not values:
return []
write = 1
for read in range(1, len(values)):
if values[read] != unwanted:
values[write] = values[read]
write += 1
return values[:write]This version deletes each no-read with pop, then always moves on:
i = 0
while i < len(scans):
if scans[i] == -1:
scans.pop(i)
i += 1 # bug: the next scan has slid into slot i, and this skips itCounterexample: [52, -1, -1, 18]. The bug returns [52, -1, 18]. The correct answer is [52, 18].
When the first -1 is deleted, the second -1 slides into slot 1. Then i moves to slot 2, so the second -1 is never checked. The first attempt avoids this, because it moves on only when the scan stays. It is also still O(n^2), because each pop slides the rest of the list.
Deleting while walking forward skips the next value, because it slides into the index you left behind.
This version of Template 2 compares with values[read - m] instead of values[write - m]:
if read < m or values[read] != values[read - m]: # bug: compare with the kept prefix, values[write - m]Counterexample: the alert batch [1042, 1042, 1042, 1187, 2210, 2210, 2210, 2210, 3005] with m = 2. The bug returns [1042, 1042, 1187, 2210, 3005]. The correct answer is [1042, 1042, 1187, 2210, 2210, 3005].
What went wrong? At read = 5, the bug looks at slot 3. That slot held 1187 in the input, but write has already copied the first 2210 over it. So the second 2210 looks like a third copy and is dropped.
A comparison with read − m is correct only on an untouched copy of the input, and that copy costs O(n) memory. The kept prefix is the only part of the array that the pass never spoils.
This version runs Template 1 and then returns all of values.
Counterexample: the test buffer [52, -1, 37, -1, -1, 18, 64]. The bug returns [52, 37, 18, 64, -1, 18, 64]. The correct answer is [52, 37, 18, 64].
The pass never clears the slots after write. They still hold old scans, and here they even hold a no-read and two copies of scans already uploaded. Return the kept prefix, values[0..write−1], and nothing after it.
Your team lead brings a request from the label team. An oversized parcel needs two labels, so its code must appear twice in the print buffer. The buffer [48, 31, 57, 0] holds three codes and one spare slot, and 31 is oversized. The printer needs [48, 31, 31, 57].
Run read and write from the left, and write passes read. The second 31 lands on slot 2 before read has reached the 57 there. The 57 is lost: the buffer becomes [48, 31, 31, 31], and the last write falls outside the buffer.
The left-to-right pattern needs an output that is never longer than the input. Here the output grows, so run both pointers from the right instead. Read starts on the last code and write on the last slot, so write stays ahead of read. That gives [48, 31, 31, 57].
This is LeetCode 1089: Duplicate Zeros.
Your team lead brings five product changes. Each one keeps the read pointer that visits every value and the write pointer that marks the next free slot.
A monitoring system agents write a heartbeat line, code 0, every few seconds. Before shipping a buffer of log line codes, the agent removes the heartbeats in place. For [0, 503, 0, 0, 211, 503] it ships [503, 211, 503].
This is Template 1 with unwanted = 0. It is the first practice problem.
A search posting list holds sorted document ids, and a merge bug repeated some of them. The index allows at most m copies of each id. For [3, 3, 3, 3, 8, 8, 12] and m = 2, the list becomes [3, 3, 8, 8, 12].
This is Template 2. It is the second practice problem.
A Freight conveyor scanner reads one parcel again and again when the belt stalls. It sends each run as the code, then its length as a negative number when the run is longer than one. For [705, 705, 705, 812, 233, 233] it sends [705, -3, 812, 233, -2].
Read finds the end of each run, and write puts down one or two values. A run of length 2 or more never writes more values than it reads, so write still never passes read. It is the third practice problem.
A search engine logs each keystroke of a query, and a backspace is logged as #. The log trak#ck ends as the text track.
A letter is copied to slot write, and write moves on. A backspace moves write back one slot, but never below 0. Checking whether two logs end as the same text is a checkpoint problem after the next lesson (LeetCode 844: Backspace String Compare).
A payments app keeps a payment request only at the first use of its idempotency key. The keys arrive in time order, not sorted: [7, 3, 7, 9, 3]. The requests to keep are [7, 3, 9].
Template 2 with m = 1 keeps all five keys, because no two equal keys sit next to each other. Read and write still do the copying, but the test in step [3] needs a seen set from Module 3: O(n) time and O(n) memory. This is the other checkpoint problem.
Explain it back: write the idea in your own words and get feedback.
In two or three sentences, explain to a teammate why write never overwrites a value that has not been read, and why the pass is O(n). The AI tutor checks your answer against the invariant and the key points of this lesson.
Here is how you could explain it in an interview, now that you have built it.
Read visits every value once. Write marks the next free slot, and it moves only when I keep a value.
Write moves at most once for each step of read, so it never passes read. I never overwrite a value I have not read.
Each value is read once and copied at most once, so the pass is O(n) with O(1) extra memory. I return the kept prefix.
The task removes values in place and keeps the order of the rest, so I would use read and write pointers. Deleting each value and shifting the rest is correct, but every shift moves values I keep anyway. With many deletions that is O(n^2).
Instead, read visits every value once, and write marks the next free slot. When I keep a value, I copy it to the slot at write and move write on. When I skip a value, only read moves.
Write moves at most once for each step of read, so it never passes read, and nothing unread is overwritten. Each value is read once and copied at most once: O(n) time and O(1) extra memory.
At the end I return the prefix before write. For sorted values with at most m copies, I compare with the value m slots back in the kept prefix. I would test nothing removed, everything removed and a single value.
Checkpoint · Spot the pattern
Q1/3 Which request is a job for read and write pointers?
Answer: B
"Remove", "in place" and "keep the order" are the signals. The pair that fills a truck is opposite ends, and the run of stops is a sliding window (Module 5).
Q2/3 What do the read pointer and the write pointer each do?
Answer: A
From See: read walks the whole array, one slot at a time. Write moves only after a kept value is copied to its slot.
Q3/3 The buffer is [-1, -1, 52, 37], and you remove every -1. Read is on index 2, write is on 0, and 52 is kept. Why is it safe to copy 52 into slot 0?
Answer: B
Write moves at most once for each step of read, so it never passes read. Every slot at or before write has already been read. That is "Why write never passes read" in See.
Checkpoint · Cost and correctness
Q1/4 Which statement is the read and write invariant?
Answer: A
The invariant from Shape. When read reaches the end, values[0..read−1] is the whole array, so the kept prefix holds every kept value, in order.
Q2/4 A buffer of n values has many values to remove. What does each approach cost?
Answer: B
Each deletion slides every later value one slot left, so kept values move again and again. Read and write read each value once and copy each kept value once.
Q3/4 This code walks forward, deletes each -1 with scans.pop(i), and always does i += 1. What does it return for [52, -1, -1, 18]?
Answer: B
After scans.pop(1), the second -1 slides into slot 1, but i moves on to slot 2. The second -1 is never checked. This is Exhibit 1 in Stretch.
Q4/4 Template 2 keeps at most m copies of each sorted value. Why does it compare with values[write - m] and not with values[read - m]?
Answer: A
On the alert batch [1042, 1042, 1042, 1187, 2210, 2210, 2210, 2210, 3005] with m = 2, the read − m version drops the second 2210, because slot 3 was already overwritten. This is Exhibit 2 in Stretch.
Checkpoint · Limits and variations
Q1/3 Template 1 runs on [-1, -1, -1] and removes every -1. What does it return?
Answer: A
Read visits all three slots, but nothing is kept. The kept prefix, values[0..write−1], is empty. This is the "Everything is removed" edge case.
Q2/3 Payment requests arrive in time order with idempotency keys [7, 3, 7, 9, 3]. You must keep only the first use of each key, in arrival order. What do you do?
Answer: B
The keys are not sorted, so equal keys do not sit next to each other. Template 2 with m = 1 keeps all five. Sorting would lose the arrival order. A seen set from Module 3 gives [7, 3, 9].
Q3/3 Template 1 runs on [52, -1, 37, -1, -1, 18, 64]. At the end, the array holds [52, 37, 18, 64, -1, 18, 64] and write is 4. What do you return?
Answer: B
The kept prefix ends at slot write − 1. The slots after it still hold old values, including a no-read. This is Exhibit 3 in Stretch.
Ten questions on this lesson. Pass with 80% or more to complete the lesson; the checkpoint that opens the gate comes after the next lesson, Three-way partition.
| Step | Problem | Product line | Level | What it practises |
|---|---|---|---|---|
| 1 | Compact the log buffer | Observe | Easy | Template 1: skip code 0, return the kept prefix |
| 2 | Trim repeated postings | Search | Medium | Template 2: compare with ids[write - m] in the kept prefix |
| 3 | Compress repeated scans | Freight | Medium | Read a whole run, write a code and a negative length |
Review cards from this lesson.
Five cards from this lesson join your spaced-review deck. The first one comes back tomorrow.
Step through the idea on your own input, then build it in the lab. Runs in your browser. Step through it.
A few questions, then explain the idea back in your own words. Your tutor marks it. Quiz and explain back.