Four classic interview problems on counting readings, picking non-neighbours, splitting a query and listing subsets, in our own wording. Decide first whether to count or to list.
Dynamic programming and backtracking fill the hardest slot of many coding rounds, and the interviewer rarely names either. The story talks about a courier, a code string or a search query. You must hear whether it asks for a best value, a count, a yes or no, or every option.
This set collects four classics that the launch districts do not cover yet. Each one is our own version of a well-known interview problem, so you can practise the classics without memorising someone else's wording.
Interviewers look for three things in these problems.
A first idea for the courier is to take every other door. It feels like a safe way to avoid neighbours, and it passes the example.
Exercise · Break it: The doors that skip too little. A courier may not take two orders at neighbouring doors, and wants the largest total tip. This takes every other door, starting at the first or at the second, and returns the better total. Find an input where it is wrong.
def best_tips(tips):
even_total = 0
odd_total = 0
for i in range(len(tips)):
if i % 2 == 0:
even_total += tips[i]
else:
odd_total += tips[i]
return max(even_total, odd_total)Each problem gives itself away with a signal in the statement. Here are the signals of the four problems, with the move and the Academy lesson that teaches it.
| Signal in the statement | The move | Academy lesson |
|---|---|---|
| the largest total, no two neighbours | table: take it or skip it, from the entries one and two back | DP from recursion |
| the number of ways, a string cut into pieces | table of counts, adding one or two earlier entries | One-dimensional DP |
| yes or no, a string cut into known pieces | table of booleans over positions | String DP, Memoisation teaser |
| every subset or combination, in a page | backtracking: take, recurse, undo | Subsets and permutations, Backtracking and pruning |
The Academy lessons teach each table and each search on its own. This set mixes them, so the skill you practise is choosing. Read the statement, find the signal, and say the state in words before you write a line.
For the three tables the plan is the same: the state, the transition from earlier entries and the base case. When each entry needs only the last two, two variables replace the table.
Exercise · Predict the output: Two variables instead of a table. The best total of the first i doors needs only the totals for i - 1 and i - 2, so two variables are enough. What does this print?
tips = [4, 1, 1, 9, 2]
skip = 0
keep = 0
for tip in tips:
best = max(keep, skip + tip)
skip = keep
keep = best
print(keep)The best total of the first i doors is the larger of skipping door i and taking it with the total from two doors back.
The count of readings is the same table with sums. A piece is a number from 1 to 26 without a leading zero. So a digit adds the entry before it when it is not 0. The last two digits add the entry two before when they read 10 to 26.
Exercise · Put it in order: The table of readings. Arrange the lines into a function that counts the ways to cut a string of digits into pieces from 1 to 26 without a leading zero.
def count_readings(code):
n = len(code)
ways = [0] * (n + 1)
ways[0] = 1
for i in range(1, n + 1):
two = int(code[i - 2:i]) if i >= 2 else 0
if code[i - 1] != "0":
ways[i] += ways[i - 1]
if 10 <= two <= 26:
ways[i] += ways[i - 2]
return ways[n]
ways[0] = 0A yes or no table has the same shape with booleans. The base case is true, because an empty start needs no words, and the answer is the entry for the whole query.
A table over the first i letters has n plus one entries, so the whole query is entry n, and entry n minus one answers a different question.
Exercise · Find the bug: The cell that answers the question. This says whether the query can be cut into known words, each used any number of times. splittable[i] says whether the first i letters can be cut. Which line is wrong?
def can_split(query, words):
n = len(query)
splittable = [False] * (n + 1)
splittable[0] = True
for i in range(1, n + 1):
for word in words:
start = i - len(word)
if start >= 0 and splittable[start] and query[start:i] == word:
splittable[i] = True
return splittable[n - 1]The first three are easy and each uses one idea: a take-or-skip table, a yes or no table, and a search that lists subsets. The last is medium and is the table of counts, where zeros and the range 10 to 26 decide. The minutes are time boxes for practice, close to what an interview allows.
Three of the twists are worth knowing before you start. The doors need the best total of the first i doors, so the first entries use the empty start. The query needs every earlier position that has a known word ending at the current one. And the subsets are far too many to return, so the list comes back as a page: the list for 40 flags has 1,099,511,627,776 rows.
In an interview, say the state out loud first: "ways of i is the number of readings of the first i digits". It shows the interviewer your thinking, and it catches a wrong base case early.
The four time boxes add up to 80 minutes. Split them into two sittings: the first three problems take 50 minutes, and the decode problem takes 30.
If you finish a problem early, use the rest of its box well. Explain the table aloud in one minute, as you would at the end of an interview, and say its cost in time and memory.
Checkpoint · Dynamic programming and backtracking
Q1/5 A story asks for the number of ways to cut a 60-digit code into valid pieces. Which move fits?
Answer: B
A 60-digit code can have 2,504,730,781,961 readings, far too many to list. A table counts them in 60 steps.
Q2/5 Tips are 5, 1, 1, 5 at four doors in a row, and no two neighbouring doors may both be taken. What is the best total?
Answer: B
The first and the last door are not neighbours, so 5 + 5 = 10. Taking every other door gives only 6.
Q3/5 A word table has entries splittable[0] to splittable[n] for a query of n letters. Which entry says whether the whole query can be cut?
Answer: C
Entry i describes the first i letters, so the whole query is entry n. Entry 0 is the empty start, which is true.
Q4/5 In the table of readings, what does a digit 0 add on its own?
Answer: B
A piece is a number from 1 to 26 without a leading zero. A zero only helps as the second digit of 10 or 20.
Q5/5 A take-or-skip recursion for 35 doors uses no table. About how many calls does it make?
Answer: C
The recursion solves the same later doors again and again. A table solves each of the 36 positions once.
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.