← Questions
Python fundamentals: a Fibonacci generator, a balanced BST and frozenset
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Waymo's technical phone screen, last asked Feb 2026. It tests python, generators and trees.
A set of short Python tasks, each followed by questions on how it works.
Part 1: Fibonacci generator
Unless you agree on a different interface with the interviewer, assume:
-
fib() -> Iterator[int]— yields0, 1, 1, 2, 3, 5, ...forever.
from itertools import islice
list(islice(fib(), 8)) -> [0, 1, 1, 2, 3, 5, 8, 13]
Explain what yield does to the function, when its body runs, and what happens on next() after the generator returns.
Part 2: balanced binary search tree
- A
BSTclass withinsert(key),contains(key) -> boolandinorder() -> listthat stays balanced (height O(log n)) as keys are inserted in any order, for example an AVL tree with rotations.
t = BST()
for k in [1, 2, 3, 4, 5, 6, 7]: t.insert(k)
t.inorder() -> [1, 2, 3, 4, 5, 6, 7]
t.height() -> 3 # a plain BST would have height 7
t.contains(4) -> True
Part 3: frozenset
Explain frozenset: how it differs from set, why it can be a dictionary key or a member of another set, and when you would choose it.
s = {frozenset({1, 2}), frozenset({2, 1})}
len(s) -> 1
{frozenset({1, 2}): "a"} # allowed; {set(): "a"} raises TypeError
Clarifying questions
- Should the generator start at 0 or 1?
- Is a self-balancing tree required, or is building a balanced tree from a sorted list enough?
- Are duplicate keys allowed in the tree?
What a strong answer covers
- A generator holding two variables, lazy and O(1) memory;
StopIterationafter return. - The balance invariant (AVL heights differ by at most 1), the four rotation cases, and height updates on the way back up; O(log n) insert and lookup. Building from a sorted list by taking the middle element as the root, as the simpler alternative.
- Hashability:
frozensetis immutable, so its hash is stable; use it for set-valued dictionary keys, sets of sets, and constant lookup tables. - Tests for each part, including sorted inserts for the tree.
Follow-ups
- Make
fibaccept a limit, and compare a generator with a list for memory. - Add
deleteto the tree. - What breaks if a mutable object's hash changes while it is in a set?
Where it's asked
- Waymo · Technical phone screen · Last asked Feb 202645 min · Coding: a classic algorithm problem or an original, under-specified problem framed around self-driving
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
