nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound

AI-powered interview preparation platform. Practice with curated questions, mock interviews, and personalized learning paths to crack your dream tech interview.

Quick Links

  • Technologies
  • Mock Interviews
  • Saved Questions
  • Pricing

Company

  • About Us
  • Contact Us

Legal

  • Privacy Policy
  • Terms of Use

© 2026 nextRound. All rights reserved.

Questions
10 of 18
1Why is using a mutable default argument (e.g., `def f(x=[])`) considered a common pitfall?
2What is the purpose of the `nonlocal` keyword, and how does it differ from `global`?
3How does Python's `for` loop differ conceptually from a C-style `for` loop?
4How does structural pattern matching (`match`/`case`, introduced in Python 3.10) differ from a chain of `if`/`elif` statements?
5What are first-class functions, and how does Python's treatment of functions as objects enable higher-order functions?
6What is the purpose of the `else` clause on `for` and `while` loops, and when does it execute?
7How would you implement memoization for a recursive function, and what tradeoffs exist between manual caching and `functools.lru_cache`?
8What is the difference between positional, keyword, default, `*args`, and `**kwargs` parameters?
9Explain Python's LEGB rule for variable scope resolution.
10How does Python's `for` loop differ conceptually from a C-style `for` loop?
11What are first-class functions, and how does Python's treatment of functions as objects enable higher-order functions?
12How would you implement memoization for a recursive function, and what tradeoffs exist between manual caching and `functools.lru_cache`?
13What is the purpose of the `nonlocal` keyword, and how does it differ from `global`?
14What is the difference between positional, keyword, default, `*args`, and `**kwargs` parameters?
15Why is using a mutable default argument (e.g., `def f(x=[])`) considered a common pitfall?
16How does structural pattern matching (`match`/`case`, introduced in Python 3.10) differ from a chain of `if`/`elif` statements?
17What is the purpose of the `else` clause on `for` and `while` loops, and when does it execute?
18Explain Python's LEGB rule for variable scope resolution.
PythonPython
Basics
Control Flow and Functions
Data Structures
Comprehensions & Functional Programming
Iterators, Generators & Decorators
Object-Oriented Programming
Exception Handling & Debugging
Concurrency & Parallelism
Performance & Optimization
Testing
Security
Modules, Packaging & Environment
Type Hinting & Modern Python
System Design & Architecture with Python
Best Practices & Design Patterns
Edge Cases & Tricky Interview Questions
10 / 18

How does Python's `for` loop differ conceptually from a C-style `for` loop?

Difficulty: 3/10
Control Flow & Loops, Iteration Protocol, Iterables vs Iterators

Iterator-driven for loops vs counter-driven for loops

A C-style for is three expressions around a counter: initialize, test, increment. It is syntactic sugar over a while loop and the index is the point of the construct. Python's for is a for-each over the iterator protocol: it calls iter() on the object once, then repeatedly calls next() on the resulting iterator, binds each value to the loop target, and stops when StopIteration is raised. There is no counter in the language construct at all; range(n) is simply an ordinary lazy iterable that happens to yield integers.

javascript

Why the difference matters: because for works on any iterable, the same loop handles lists, dicts, files, generators, database cursors and infinite streams, and it can be lazy and constant-memory. The loop code does not know or care how items are produced. The idiomatic replacements for index juggling are enumerate (index and value), zip (parallel iteration; zip(..., strict=True) from 3.10 raises on length mismatch), reversed, and sorted. Writing for i in range(len(x)): x[i] is the classic sign of C or Java habits.

Trade-offs and alternatives: you still want an index-based loop when you genuinely need to mutate by position, skip ahead, or walk two cursors at once; in that case a while loop with an explicit iterator or range with a step is clearer than contortions. Comprehensions and generator expressions are better than loops when you only build a new collection. Common mistakes: expecting that reassigning the loop variable affects the iteration (it doesn't), modifying a list while iterating over it (elements are skipped; for dicts and sets, changing size raises RuntimeError), expecting the loop variable to be scoped to the loop (it persists after the loop, and is undefined if the iterable was empty), and iterating a generator twice, because an iterator is exhausted after one pass.

Version notes: zip(strict=True) needs 3.10+, and async for with aiter/anext is the asynchronous variant of the same protocol. The semantics of the basic for statement have been stable across Python 3.

Scenario Questions

0-2 years experience

  1. 1Why does for x in nums: x = x * 2 leave the list nums unchanged? How would you actually double every element?
  2. 2What is the value of i after for i in range(3): pass finishes? What happens if the range is empty?

2-5 years experience

  1. 1A teammate removes items from a list inside a for loop and some items are not processed. Explain exactly why, and give two correct ways to filter in place or by copy.
  2. 2You need the index and value of each item and also want to loop over two lists in parallel. Which built-ins would you use instead of range(len(...)), and what does zip do with unequal lengths?

5-8 years experience

  1. 1Implement your own iterable class that can be used in a for loop. How do you distinguish an iterable from an iterator in your design, and why can't a plain generator be iterated twice?
  2. 2You must process a 50 GB log file with constant memory. How does the for protocol help, and where do people accidentally load everything into memory?

8+ years experience

  1. 1Explain how a for loop is compiled to bytecode (GET_ITER and FOR_ITER) and why iterating a list with for is faster than an equivalent while loop with an index.
  2. 2Design an async data-ingestion pipeline. How does async for differ from for (the __aiter__ and __anext__ protocol), and what goes wrong if you mix blocking iterators into an event loop?

Follow-up Questions

  • What is the difference between an iterable and an iterator, and why can a generator only be consumed once?
  • Why does modifying a list while iterating over it skip elements, and what should you do instead?
Sharethis question

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.