What this Python scope interview question tests
This is an easy Python question designed to verify your understanding of variable scope and name resolution. Quant firms ask questions like this to confirm that candidates understand how Python's LEGB rule (Local, Enclosing, Global, Built-in) governs which variable a name refers to at any given point in the code.
To answer correctly, you need to trace through the code and determine which binding each variable name resolves to as the interpreter executes. Pay close attention to where variables are assigned, whether they are redefined in nested scopes, and how those definitions affect what the print statements output. This kind of clarity is foundational for writing predictable code and debugging scope-related bugs in production systems.
- Local vs. global variable assignment
- The LEGB name-resolution order
- Variable shadowing
- The effect of assignment on scope