TGViewer
Coding Interview Preparation Coding Interview Preparation @coding_interview_preparation · 5.9K subscribers
Post #1418 272
🔍 GUESS THE OUTPUT #6 - full breakdown

python
def outer():
x = 10
def inner():
print(x)
x += 1
inner()

outer()


Answer: UnboundLocalError: local variable 'x' referenced before assignment

This one surprises a lot of people - it does NOT print 10. Here's why: because inner() assigns to x (x += 1), Python treats x as a LOCAL variable throughout the entire function the moment it sees any assignment to it, even before that line executes. So the print(x) line is trying to read a local x that hasn't been assigned yet.

Fixed version:
python
def outer():
x = 10
def inner():
nonlocal x
print(x)
x += 1
inner()


The nonlocal keyword tells Python "this x refers to the enclosing function's variable, not a new local one."

This is a genuinely tricky one - Python decides variable scope at compile time based on whether a name is assigned anywhere in the function, not based on execution order. Great interview question for testing real understanding of closures vs. just pattern-matching syntax.
More from @coding_interview_preparation
  1. Oct 8, 2026If you're prepping for system design interviews, this repo is gold It contains a curated,…
  2. Oct 6, 2026document post
  3. Oct 4, 2026💼 Why Your Resume Gets Rejected Before a Human Reads It You may have good skills and proj…
  4. Oct 2, 2026🧠 Coding Myths You Should Stop Believing There's a lot of advice online about learning to…
  5. Oct 1, 2026Most Asked Topics in AI Engineer Interviews Based on 2026 candidate reports
  6. Sep 30, 2026💼 What Companies Actually Look For in a Fresher Think companies only care about your CGPA…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →