locals()
What this preview is
locals() is a hard quant interview question on language knowledge in Python.
- Difficulty
- Hard
- Topic
- Language Knowledge
- Discipline
- Quant development
- Language
- Python
- Companies
- 0
What this Python scoping and namespace question tests
This is a medium-difficulty Python question that probes your understanding of local scope and how the locals() function behaves within function execution. It's the kind of question interviewers use to assess whether you have a solid grasp of Python's name-binding mechanics — knowledge that matters when debugging unexpected variable behaviour or reasoning about code that manipulates its own namespace.
To answer correctly, you need to understand the timing of when locals() captures the current scope, how assignments interact with the local namespace dictionary, and what gets reflected in that dictionary at different points in execution. The question rewards careful reading of Python's scoping rules and awareness of how built-in introspection functions report on live state.
- Local vs. global scope and the LEGB rule
- The behaviour of
locals()inside function bodies - How variable assignments update the local namespace
- Distinction between the namespace dictionary and actual variable binding
Related practice
Unlock full access to getcracked
Join to unlock this question, detailed solutions, and our complete library of quant finance interview prep.