← Back to videos

Junior Python interview: branching, if/else, dictionaries and an unexpected catch

Interview timeline Click any block to jump

Interview breakdown

Interviewer verdict
Passed with caveats Level: Junior (solid practical code reading despite outdated/shaky theory)

Passed conditionally for a Junior bar: the candidate made frequent theoretical errors (missing match/case from Python 3.10+, confusing the ternary operator with +=, overgeneralizing lazy evaluation in if), but steady communication and strong reasoning on the tricky dict-key-overwriting snippet outweighed those gaps. Reaching Middle will require refreshing modern Python syntax and articulating clear tool-selection trade-offs.

Candidate: Egor
Target role: Junior Python Developer

34 y/o · school/university background in Pascal, Delphi & C++, former sysadmin and business owner; studying Python on and off for ~2 years to switch careers

Answers accuracy 12 total
54% score
Theory
2/8 correct · 50%
Live coding
2/4 correct · 62%
Practical task
Solved

Branching via a {...}[True] dict literal with boolean key overwriting

The candidate initially overlooked the trailing [True] subscript on the dict literal, but already traced key collisions on True and False accurately and spotted [True] immediately upon seeing the output. When tested with x = True, he predicted on the first try that x > 0 would overwrite isinstance(x, bool) and deduced why if/elif is both faster and bug-free here.

Topics covered Click a topic to filter questions

Candidate strengths

  • + Strong practical code reading: accurately traced duplicate boolean key evaluation and overwriting inside the {...}[True] dictionary literal
  • + Correctly predicted the x = True trap before running it (True > 0 overwriting the earlier isinstance(x, bool) entry) and updated his performance comparison accordingly
  • + Accurate explanation of short-circuit evaluation in chained 'and' and 'or' boolean expressions
  • + Fluent, composed communication without awkward freezes in his very first Python interview

Areas to improve

  • → Update knowledge of modern Python syntax, including match / case structural pattern matching (Python 3.10+) and ternary expressions (x if cond else y)
  • → Review exact operator terminology (unary, binary, ternary, and augmented assignment +=)
  • → Develop clear engineering criteria for choosing between if/elif, dictionary dispatch tables, and match/case
  • → State 'I don't know' directly instead of circling around unfamiliar theoretical questions

Questions in this interview

12 / 12
01 ▶ 05:27
How is conditional branching written with if / elif / else in Python, and is there a limit on branches?
Theory Control flow, loops & syntactic sugar
?
Candidate's answer

Described if, elif (briefly saying else if from C++), and else along with nested conditionals, noting that branch count is practically limited by code readability.

Key takeaway

In an if / elif / else chain, conditions are tested top-to-bottom until the first truthy branch runs, skipping all remaining branches.

Open moment on YouTube ↗
#01
02 ▶ 07:31
What is spaghetti code, and how should you refactor deeply nested or sprawling conditionals?
Theory Algorithms, code reading & engineering practices
?
Candidate's answer

Had not heard the term 'spaghetti code' before, but intuited it as deeply tangled nesting and suggested simplifying checks with 'in' membership tests, helper functions, and dropping redundant else blocks.

Key takeaway

Deeply nested conditionals are flattened using guard clauses (early returns), set membership checks, dictionary dispatch tables, or polymorphism.

Open moment on YouTube ↗
#02
03 ▶ 14:13
What are unary, binary, and ternary operators in Python?
Theory Control flow, loops & syntactic sugar
?
Candidate's answer

Confused operator arity terms: called += a ternary operator, then called a += 1 unary, only correctly identifying two-operand operators like 'or' and 'and' as binary.

Key takeaway

Operator arity counts operands: unary takes one (-x, not x), binary takes two (a + b), Python's sole ternary expression takes three (x if cond else y), while += is an augmented assignment statement.

Open moment on YouTube ↗
#03
04 ▶ 17:33
What is a switch / case construct, and does modern Python have an equivalent?
Theory Control flow, loops & syntactic sugar
?
Candidate's answer

Explained C-style switch / case accurately, but mistakenly stated that Python still has no built-in equivalent.

Key takeaway

Python 3.10 introduced match / case (PEP 634) for structural pattern matching, which goes beyond C's switch by destructuring sequences, mappings, and class instances.

Open moment on YouTube ↗
#04
05 ▶ 20:29
How does short-circuit evaluation work in boolean conditions, and is an 'if' condition always lazy without 'and' / 'or'?
Theory Control flow, loops & syntactic sugar
?
Candidate's answer

Explained short-circuiting of 'or' and 'and' chains accurately, but stubbornly insisted that 'if' itself always performs lazy evaluation even on a single arithmetic or equality expression.

Key takeaway

Short-circuit laziness comes from 'and'/'or' operators and skipping untaken if/elif branches, whereas a single expression like if 1 + 2 + 3: is evaluated eagerly in full.

Open moment on YouTube ↗
#05
06 ▶ 31:03
How can you implement conditional dispatch using a dictionary in Python?
Theory Collections, lists, dicts & hash tables
?
Candidate's answer

Described the dispatch-table pattern: mapping keys to target values or handler functions and looking up the action via key or dict.get(key, default).

Key takeaway

Mapping keys to callables in a prebuilt dict (handlers.get(action, default)()) cleanly replaces long equality chains with O(1) lookup.

Open moment on YouTube ↗
#06
07 ▶ 33:07
How do you decide when to use if / elif / else versus a dictionary dispatch table?
Theory Algorithms, code reading & engineering practices
?
Candidate's answer

Had the right intuition (if/else for general boolean conditions, dict for fixed key sets), but struggled under pushback to state a crisp decision rule.

Key takeaway

Dict dispatch works when mapping exact hashable keys (especially in a preallocated table), whereas if/elif is required for inequalities, type checks, and arbitrary predicates.

Open moment on YouTube ↗
#07
08 ▶ 39:06
Is branching via a dictionary always faster than an if / else statement?
Theory Collections, lists, dicts & hash tables
?
Candidate's answer

Initially claimed dict lookup beats even a simple 'if a == 1', overlooking hashing and dict-creation overhead, though he suggested benchmarking with timeit and later corrected himself during live coding.

Key takeaway

For a few branches, if/elif bytecode jumps beat hashing overhead; a dict wins on many exact keys only when prebuilt once rather than reconstructed on every call.

Open moment on YouTube ↗
#08
09 ▶ 43:14
What does {isinstance(x, bool): 'булево', x < 0: 'отрицательное', x == 0: 'ноль', x > 0: 'положительное'}[True] return for a positive integer?
Live coding Collections, lists, dicts & hash tables
?
Candidate's answer

Accurately traced all four key expressions and how duplicate False keys overwrite earlier values to leave {False: 'ноль', True: 'положительное'}, briefly overlooking the trailing [True] lookup before immediately catching it.

Key takeaway

A dict literal evaluates all key-value pairs top-to-bottom and overwrites values on duplicate keys, after which [True] indexes the final value bound to True.

Open moment on YouTube ↗
#09
10 ▶ 52:17
What happens when x = True is passed into the same {...}[True] dictionary snippet?
Live coding Data types, numbers & memory
?
Candidate's answer

Predicted 'положительное' instead of 'булево' on the first try: isinstance(True, bool) sets True: 'булево', but eager evaluation reaches True > 0 (since True == 1), which also evaluates to True and overwrites the entry.

Key takeaway

Because bool subclasses int (True == 1, False == 0), x = True satisfies both isinstance(x, bool) and x > 0, so eager dict construction silently overwrites the first match.

Open moment on YouTube ↗
#10
11 ▶ 53:54
Which is faster and better suited for classifying x here — if / elif or the {...}[True] dictionary trick?
Live coding Control flow, loops & syntactic sugar
?
Candidate's answer

Revised his earlier claim and rightly concluded that if / elif is faster here because {...}[True] allocates a new dict and evaluates every condition eagerly on each call.

Key takeaway

The {cond1: v1, cond2: v2}[True] anti-pattern allocates a throwaway dict, evaluates all conditions eagerly, and inverts branch priority so the last True wins instead of the first.

Open moment on YouTube ↗
#11
12 ▶ 56:15
What logical bug would you flag in this snippet during code review regarding the isinstance(x, bool) branch?
Live coding Algorithms, code reading & engineering practices
?
Candidate's answer

Noted that overwriting keys felt awkward, but said he had no major review objections and missed that the 'булево' branch is completely dead code.

Key takeaway

Placed first in {...}[True], isinstance(x, bool): 'булево' is 100% dead code: True is overwritten by x > 0, False is overwritten by x == 0, and non-numeric inputs raise TypeError on x < 0.

Open moment on YouTube ↗
#12
Original video description & timecodes

Egor is taking a trial interview for the role of Junior Python Developer. In this episode, we discuss branching in Python, the if/else construct, the ternary operator, match/case, lazy evaluation and alternatives using dictionaries.

In the practical section, we analyse a tricky piece of code involving a dictionary, Boolean keys and value overwriting.

Timestamps:

00:00 — Start
00:27 — Introductions and the interview format
05:27 — What is branching in Python
07:31 — Spaghetti code and nested conditions
10:43 — What to do if there are too many conditions
14:17 — The ternary operator
17:33 — switch/case and whether it exists in Python
20:29 — Lazy evaluation in conditions
31:03 — Branching using a dictionary
33:07 — How to choose between if/else and a dictionary
39:06 — Which is faster: if/else or a dictionary
43:02 — Practical exercise with code
57:01 — Candidate’s self-assessment
01:00:51 — Feedback on the interview