Python for Interviews

Core Syntax & Functions

Types, operators, conditionals, loops and functions that return values.

Easy, about 60 minutes to practise.

1Understand

The idea

Interview solutions are functions: they take inputs and return a value, they never print it. Get comfortable with // (floor division) and % (remainder), with range(start, stop, step) bounds, and with break/continue to control a loop. Most bugs at this level are off-by-one bounds and returning too early from inside a loop.

Intuition

Interview code is almost always a function: inputs in, one answer out via return. Printing is for debugging only; the grader never reads stdout.

Floor division // and remainder % show up constantly (digits, cycling, even/odd). range(start, stop, step) never includes stop, so off-by-one bugs are usually wrong bounds or advancing past the last useful index.

break exits the loop; continue skips to the next iteration. Returning from inside a loop is fine when you have found the answer, but returning too early before you have finished scanning is a classic miss.

What you will be able to do

  • Use // and % correctly
  • Write loops with break/continue and range bounds
  • Return values instead of printing

2See it

Walkthrough

Floor division and remainder

n = 17, d = 5, // and % split a number without floating point.

1 / 4

17
5
01

Start with dividend 17 and divisor 5. We want how many whole groups of 5 fit, and what is left.

n, d = 17, 5

A single loop over the digits (or over range(n)) is O(number of steps). Nested loops multiply. Prefer clear bounds and an early return only when the answer is final; otherwise finish the scan. Space is usually O(1) unless you build a list of results.

Read the walkthrough as text

Digit sum helper. Write a function that returns the sum of digits of a non-negative integer n.

  1. Start total = 0.
  2. While n > 0: add n % 10 to total, then set n = n // 10.
  3. Return total. For n = 0, the loop never runs and you correctly return 0.

In code:

  • def digit_sum(n):
  • total = 0
  • while n > 0:
  • total += n % 10
  • n //= 10
  • return total

If you print(total) instead of returning it, the function yields None and every caller breaks.

3Recognise it

Kinds of problems (4)

Same pattern, different surfaces. Each row is a problem shape you should recognise, not only the first walkthrough example.

  • Arithmetic and integer division

    Example. Split n items evenly among k people, how many full groups and how many left over?

    How. Use // for whole parts and % for remainder; know when you want float division with /.

  • Conditionals and boolean logic

    Example. Classify a score as pass/fail with an optional distinction boundary.

    How. Chain elif for ordered ranges; combine predicates with and/or/not without redundant comparisons.

  • Loops over ranges

    Example. Sum indices 0..n−1 or visit every pair (i, j) with i < j.

    How. range(stop), range(start, stop), and range(start, stop, step) control bounds; len(a) is the exclusive end index.

  • Functions with parameters and return values

    Example. Return the best index or −1 when nothing matches.

    How. Compute with local variables and return the answer; callers use the result, printing is for debugging only.

Common mistakes

Failure modes the tutor watches for. Open the Common mistake tab in the walkthrough to watch one fail live.

  • Integer vs float division

    Why it happens. Both / and // divide, so it is easy to pick the wrong one under time pressure.

    How it fails. n = 7, k = 3: n / k → 2.333 when the problem wants 2 full groups and 1 remainder (need // and %).

    Fix. Whole counts and splitting into equal parts → //; remainders → %; only use / when you need a float quotient.

  • Missing return statement

    Why it happens. Printing the answer looks like success in the notebook, but callers get None.

    How it fails. def best_index(nums): print(max(range(len(nums)), key=nums.__getitem__)), returns None to the grader.

    Fix. return the value the problem asks for; reserve print for local debugging.

  • Mutable default argument

    Why it happens. Default arguments are evaluated once, so a shared list accumulates across calls.

    How it fails. def add(x, acc=[]): acc.append(x); return acc, add(1) and add(2) return [1, 2] on the second call.

    Fix. Use None as default and acc = acc or [] inside the body, or use a fresh list literal only in the caller.

4Check yourself

Concept check

A short quiz on this lesson before you practise. Optional, and it does not gate coding.

Ready to practise?

Mastery comes from solving. The lesson is optional context.

All lessons