Three interview problems about choices along a line and across a grid: count the longest runs, split digits into ids and cross a grid of traps. The move: say what one cell of the table means.
You work on a sensor service, an id parser and a game server. The three problems on this street come from them. Each fills a table in which every cell answers one question about a prefix of a line or a corner of a grid.
Here is the question each cell answers, and which cells it reads.
| Problem | A cell means | It reads |
|---|---|---|
| Count the longest increasing runs | the longest run ending at reading i, and how many runs have that length | every earlier smaller reading |
| Ways to split a digit stream into ids | the number of splits of the first i digits | the last few cells, while the piece is a valid id |
| Least starting health to cross a grid | the health needed on entering room (r, c) | the cells to the right and below |
The table is the easy part. Writing the meaning of one cell so exactly that the formula follows is the hard part.
Exercise · Predict the output: Splitting a digit stream. Each piece must be a whole number from 1 to k with no leading zero. Cell i counts the splits of the first i digits. What does the code print?
digits = "1317"
k = 100
n = len(digits)
ways = [0] * (n + 1)
ways[0] = 1
for i in range(1, n + 1):
for j in range(max(0, i - 3), i):
if digits[j] != "0" and int(digits[j:i]) <= k:
ways[i] += ways[j]
print(ways[n])Write one sentence before the loop: this cell holds the answer to this question about this prefix or corner. Then name the filled cells that the sentence needs.
Each cell answers one question about a prefix or a corner, and it reads only cells that are already filled. Fill them in an order that keeps that true, and the answer sits in one cell.
The sentence also tells you when one table is not enough.
Cell i counts the splits of the first i digits: add the cells that a valid piece can start from.
Exercise · Find the bug: The trap that heals. This fills the table of the health needed on entering each room, from the last room backwards, and returns the least starting health. Entering a room changes health by dungeon[r][c], and health must stay at least 1. One line has a sign wrong. Which one?
def least_start_health(dungeon):
rows = len(dungeon)
cols = len(dungeon[0])
need = [[0] * cols for _ in range(rows)]
for r in range(rows - 1, -1, -1):
for c in range(cols - 1, -1, -1):
if r == rows - 1 and c == cols - 1:
after = 1
elif r == rows - 1:
after = need[r][c + 1]
elif c == cols - 1:
after = need[r + 1][c]
else:
after = min(need[r + 1][c], need[r][c + 1])
need[r][c] = max(1, after + dungeon[r][c])
return need[0][0]The Academy lessons Dynamic programming from recursion, 1-D DP and Grid DP teach the table on a plain line and a plain grid. This street changes three things.
One table is not enough. For the readings 1 3 5 4 7, the longest runs end at 1, 2, 3, 3 and 4 readings, and the counts are 1, 1, 1, 1 and 2. Only the last reading ends a longest run, and two runs reach it, through the 5 or through the 4. For 1,500 readings, the table checks 1,124,250 pairs.
The look-back is limited. For the digits 1317 and a limit of 100, the cells are 1, 1, 2, 3 and 5. With no limit they are 1, 1, 2, 4 and 8. For 100,000 digits and a limit of 10^9, each cell looks back at most 10 digits, about 1,000,000 steps.
Looking back to the start would take up to 5,000,050,000.
The table runs backwards. In a grid with rows -2 -3 3, -5 -10 1 and 10 30 -5, the filled table starts with 6 in the last cell and ends with 7 in the first. A room's need depends on the rooms after it, so those cells must be filled first.
Counting the readings that end a longest run misses runs: add the counts of those ends instead.
Exercise · Break it: Counting the ends. This counts the increasing runs of the longest length. It fills a table of lengths and a table of counts. Find an input where it gives the wrong answer.
def count_longest(readings):
n = len(readings)
length = [1] * n
ways = [1] * n
for i in range(n):
for j in range(i):
if readings[j] < readings[i]:
if length[j] + 1 > length[i]:
length[i] = length[j] + 1
ways[i] = ways[j]
elif length[j] + 1 == length[i]:
ways[i] += ways[j]
best = max(length)
return sum(1 for i in range(n) if length[i] == best)Solve them in this order. The first keeps two tables, the second limits its look-back, and the third fills the grid backwards.
Before you code each problem, write one sentence: this cell holds the answer to this question. If you need two facts about the same prefix, plan two tables.
The runs problem is the easiest start, since its table is one line. The splits problem adds a rule about the pieces, and the grid problem adds a second dimension and a backwards fill.
Give each problem about 25 minutes. Spend the first five on the sentence and a table filled by hand for a line of five items or a grid of three by three.
Test one item, a line that cannot be split at all, and an input where every choice is equal. Most wrong answers come from a cell that mixes two meanings, such as a length and a count in one number.
Checkpoint · DP on lines and grids
Q1/5 For the digits 1317 with a limit of 100, how many splits into whole numbers from 1 to 100 are there?
Answer: C
The cells are 1, 1, 2, 3 and 5. The pieces 131, 317 and 1317 are above 100, so only 5 splits remain. With no limit there would be 8.
Q2/5 For the readings 1, 3, 5, 4, 7, how many increasing runs have the longest length?
Answer: B
The longest length is 4, and two runs reach it: 1, 3, 5, 7 and 1, 3, 4, 7. Counting only the readings that end a longest run would give 1.
Q3/5 Why does the longest-runs table need a second table of counts?
Answer: B
The count table records how many runs have the best length ending at each reading, and the answer adds those counts.
Q4/5 Why is the least starting health table filled from the last room back?
Answer: B
The health needed on entering a room depends on the cheapest route after it, so the cells to its right and below must be filled first.
Q5/5 A single room changes health by -3. What is the least starting health?
Answer: D
Health must be at least 1 after the room, so you need 1 - (-3) = 4 before it. Adding the change instead of subtracting gives 1.
Review cards from this lesson.
A few questions, then explain the idea back in your own words. Your tutor marks it. Quiz and explain back.