Start from a blank editor

50 min4 partsFoundationFundamentalsChecked Dec 2026
Start the lessonShort steps, each with something to do
What you'll learn
  • Start any coding question with the same five moves, so an empty editor never stalls you
  • Write the plainest correct version first, run it on the examples, and read what fails
  • Make a slow answer fast with a set of what you've seen, and say why it's faster
  • Use the three moves most interview answers lean on, counting, grouping and sorting with a key
  • Build a class one method at a time, and add a level without breaking the one before
Part 1 of 4about 19 min
Five moves

A blank editor, and the first five minutes

Every coding round starts the same way: a short question, a few examples, and an empty editor. Here is one, the way a screen gives it:

Write first_repeat(ids): the first order ID that appears a second time in the list, or None if no ID does.

CallReturns
first_repeat([7, 3, 9, 3, 7])3
first_repeat([1, 2, 3])None

Most people freeze here because they try to write the final answer first. Strong candidates don't. They make five moves, in the same order every time, and the code follows from them:

  1. Say it back, with an example of your own.
  2. Write the examples as checks, so you can run them at any moment.
  3. Write the plainest version that's correct, however slow.
  4. Run it, read what fails, and fix it.
  5. Make it faster, only if the input needs it, and say what it costs.

This lesson makes those moves on four small questions, with less help each time, so that by the last one the editor holds nothing but a signature. 9 steps end with a small task in the editor. None of it is advanced Python: the point is the habit. Every lesson after this one starts with a question like first_repeat, and the first five minutes go the same way.

Say it back, then write the examples as checks

Saying the question back in your own words is how you find what it really asks. "The first ID that appears a second time" doesn't mean the ID that comes first in the list. In [7, 3, 9, 3, 7], 7 comes first, but 3 is the first one to come back: its second copy sits at position 3, and 7's second copy only at position 4. So the answer is 3.

Predict

Say it back on your own example: what does first_repeat([5, 1, 5, 1]) return?

Pick an answer above to see the rest

Write the plainest version that's correct

The plainest version follows the words of the question. Walk the list, and at each position look back at the IDs before it. The first ID you find among them is the answer. If the walk ends, nothing came back.

It looks at about half of all pairs, so it's slow on a long list. Write it anyway. It gets the checks passing, it gives you something to compare a faster version against, and if time runs out, a slow answer that works beats a fast one that doesn't.

ids[:j] is the part of the list before position j, and x in some_list asks whether x is anywhere in it.

In the editor, write first_repeat(ids) the plain way. The checks print ok for each example that passes.

Run it, and read what fails

Running isn't the last move. It's how you find the mistake you didn't know you made. A failing check says three things: which input, what you got, and what it expected. Read all three before you change a line.

Here a classmate's version has one bug. Every call returns the first ID in its list, even [1, 2, 3], where nothing repeats. When every answer is wrong the same way, look for the line that runs first, at position 0.

In the editor, run first_repeat(ids) as it is, read the failures, and fix the one line that's wrong.

Make it faster with a set of what you've seen

On 100,000 IDs, looking back at every earlier ID means about 5 billion comparisons. That's minutes. The fix is to remember what you've seen in a set: asking whether a set holds something takes about the same time however big the set is, while in on a list reads it item by item.

seen = set()
print(3 in seen)
seen.add(3)
seen.add(3)
print(3 in seen, len(seen))
False
True 1

A set holds each value once, and in answers at once. So one pass is enough: for each ID, ask whether it's in the set, and only then add it. Interviewers want the cost in big-O notation, which says how the work grows with n, the length of the input. One pass is O(n) time: twice the IDs, twice the time. Looking back at every earlier ID is O(n²): twice the IDs, four times the time. The set also takes O(n) memory. Say both costs out loud. That's move 5.

In the editor, write first_repeat(ids) in one pass with a set. The same checks must still print ok.

Part 1 done
Part 2 of 4about 15 min
Three moves you'll use everywhere

Count with a dict, then pick the top

Three short moves turn up in most interview answers: counting, grouping and sorting with a key. Each is a few lines once you've written it a couple of times.

Counting is one line in a loop. counts.get(ch, 0) is the count so far, 0 for a letter not seen yet:

counts = {}
for ch in "baba":
    counts[ch] = counts.get(ch, 0) + 1
print(counts)
print(max(counts, key=counts.get))
print(min(counts, key=lambda ch: (-counts[ch], ch)))
{'b': 2, 'a': 2}
b
a

max(counts, key=counts.get) picks the letter with the highest count, and on a tie it keeps the first one it met, b. When a question says how to break a tie, put the rule in the key. lambda ch: (-counts[ch], ch) is a small function written in place: it takes a letter and gives back a pair, the letter's count made negative and the letter itself. min picks the smallest pair. The minus sign makes the highest count the smallest, and on a tie the letter decides, so the tie goes to a. collections.Counter counts in one call too, and it's worth knowing, but the dict version is the one you can always write.

In the editor, write most_ordered(orders): count the orders, then pick the top one, with ties going to the alphabet.

Group with a dict of lists

Grouping puts each item into a list under its key. setdefault(key, []) hands you the list for that key, and makes an empty one the first time:

words = ["tent", "lamp", "tarp", "kettle", "lantern"]
by_letter = {}
for w in words:
    by_letter.setdefault(w[0], []).append(w)
print(by_letter)
{'t': ['tent', 'tarp'], 'l': ['lamp', 'lantern'], 'k': ['kettle']}

Each list keeps its items in the order they came. collections.defaultdict(list) does the same job with by_letter[w[0]].append(w).

In the editor, write by_day(orders): each day's order IDs, grouped and in order.

Sort with a key, and break ties on purpose

sorted takes a key: a function that turns each item into the value to sort by. A tuple key sorts by its first part, then breaks ties with the second:

players = [("ana", 90), ("ben", 75), ("cy", 90), ("dee", 60)]
print(sorted(players, key=lambda p: p[1]))
print(sorted(players, key=lambda p: (-p[1], p[0])))
[('dee', 60), ('ben', 75), ('ana', 90), ('cy', 90)]
[('ana', 90), ('cy', 90), ('ben', 75), ('dee', 60)]

The minus sign puts the highest scores first, and the name breaks a tie from A to Z. Don't reach for reverse=True here: it reverses the whole key, the names included, so ties come out from Z to A.

In the editor, write top_k(scores, k): the names with the k highest scores.

Part 2 done
Part 3 of 4about 10 min
A class, one method at a time

A class keeps state between calls

Some coding rounds don't ask for one function. They ask for a small system: a store, a cache, a queue, whose methods are called one after another and share the same data. That's a class. The coding builds later in the plan are made of them, and they start the same way every time: __init__ makes the data, and every method reads or changes it through self.

class Tally:
    def __init__(self):
        self.total = 0

    def add(self, n):
        self.total += n
        return self.total


t = Tally()
t.add(2)
print(t.add(3), Tally().total)
5 0

self.total lives on the object, so the second add sees what the first one did. A new Tally() starts again at 0, because __init__ runs for each new object. Write a class like this one method at a time, and run the checks after each.

In the editor, write the three methods of Shelf, one line each.

Level 2 adds to the class you wrote

Coding builds come in levels. Each level adds to the class you already wrote, and every check from the levels before it keeps running. So read the new level first, list what it adds, and write only that. Change nothing that already works.

Here level 2 adds two methods: count, how many different titles the shelf holds, and take, which takes a title off and says whether it was there. Neither one changes add or has.

In the editor, Shelf holds level 1, and you add count and take.

Part 3 done
Part 4 of 4about 6 min
On your own

A question with only a signature

This time the editor holds the signature and a print line, nothing more. That's how the practice problems at the end of each lesson start. Make the five moves on your own:

Write longest_streak(days): days is a sorted list of different day numbers on which someone practiced. Return the length of the longest run of days in a row. longest_streak([1, 2, 3, 5, 6, 10]) returns 3.

Say it back with an example of your own, write the checks first, then the plainest version that works. If you stall, say out loud what you'd do by hand with a pencil, and write that.

In the editor, write longest_streak(days).

The five moves, every time

That's the whole method, and every lesson after this one uses it. The questions get harder, but the first five minutes don't change:

Check yourself
5 questions
  1. 1.You've read a coding question and the editor is empty. What comes first?

  2. 2.Why write the plainest correct version before a fast one?

  3. 3.In the one-pass first_repeat, why check x in seen before seen.add(x)?

  4. 4.sorted(names, key=lambda n: (scores[n], n), reverse=True): how do ties come out?

  5. 5.In a class for a coding build, where does the data that every method uses live?