Chapter 5

Coding Interview Practice

Apply Python fundamentals to common algorithm patterns.

Complexity basics

Count how work grows with input size. One loop is commonly linear, or O(n), which means the amount of work usually grows in step with the size of the input.

The goal here is not to memorize symbols but to start noticing the shape of a solution. A method that checks every item can be fine for small inputs, while a method that cuts the search space in half can become valuable when the input gets large.

EXAMPLE
Output will appear here.

Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.

Practice

Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.

Write a loop that prints every item in `[1, 2, 3]`.
EXERCISE
Output will appear here.
Replace the list with five items.
EXERCISE
Output will appear here.
Predict how many print calls happen for ten items.
EXERCISE
Output will appear here.
Write a function that prints every word in a three-word list.
EXERCISE
Output will appear here.

Two sum

Use a dictionary to remember numbers seen so far and find a complement in one pass. This is a classic example of trading a little memory for a much faster lookup.

The key idea is to remember what you have already seen instead of starting over each time. That way, the moment the matching number appears, the answer is already waiting in the dictionary.

EXAMPLE
Output will appear here.

Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.

Practice

Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.

Use the lesson function with `[2, 7, 11, 15]` and target `9`.
EXERCISE
Output will appear here.
Change the target to 13.
EXERCISE
Output will appear here.
Predict the returned indexes for `[3, 3]` and target `6`.
EXERCISE
Output will appear here.
Test `two_sum` with a new four-number list and target.
EXERCISE
Output will appear here.

Palindrome check

Two pointers can compare a sequence from both ends. This avoids checking the same positions more than necessary and gives you a pattern that appears again in many interview problems.

The useful mental picture here is symmetry. If the left and right sides match as you move inward, the string passes the test; if not, you can stop immediately.

EXAMPLE
Output will appear here.

Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.

Practice

Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.

Run `is_palindrome("level")`.
EXERCISE
Output will appear here.
Replace `level` with `hello`.
EXERCISE
Output will appear here.
Predict the result for `"radar"`.
EXERCISE
Output will appear here.
Test a word you choose and print the boolean result.
EXERCISE
Output will appear here.

Valid parentheses

A stack tracks brackets that still need a match. The newest opening bracket has to be matched first, which is why a stack fits this problem so neatly.

Try to read the code as a small bookkeeping system. Each opener is stored, and each closer asks whether the most recent opener is the correct partner.

EXAMPLE
Output will appear here.

Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.

Practice

Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.

Run `is_valid("()")`.
EXERCISE
Output will appear here.
Replace the text with `"([)]"`.
EXERCISE
Output will appear here.
Predict the result for `"{[()]}"`.
EXERCISE
Output will appear here.
Test one valid and one invalid bracket string.
EXERCISE
Output will appear here.

FizzBuzz challenge

Practice condition ordering: test the most specific case first. This looks simple, but it teaches a habit that shows up everywhere in programming.

When several rules could match, the first true rule wins. That means the order of the checks is part of the solution, not just a formatting choice.

EXAMPLE
Output will appear here.

Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.

Practice

Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.

Call `fizz_buzz(3)`, `fizz_buzz(5)`, and `fizz_buzz(15)`.
EXERCISE
Output will appear here.
Change the input from 15 to 30.
EXERCISE
Output will appear here.
Predict `fizz_buzz(7)` before running it.
EXERCISE
Output will appear here.
Loop from 1 to 10 and print each FizzBuzz result.
EXERCISE
Output will appear here.