🐍 Python for Everyone

← Back to practice bank

Chapter 5 Interview Prep 8 min read

Interview Questions: Loops

1 When would you choose a while loop over a for loop?

Use for when you know the number of iterations upfront (or are iterating over a known sequence). Use while when the loop should continue until some condition changes, and you don't know in advance how many iterations that will take — like reading input until the user types "quit".

Saying they're interchangeable in every situation — technically almost anything can be forced into either form, but the choice signals whether the iteration count is known ahead of time.

"Write a loop that keeps asking the user for a positive number until they give one." (Expect a while loop, since the number of attempts isn't known in advance.)

2 Does range(1, 10) include 10?

No. range(start, stop) always excludes the stop value. range(1, 10) produces 1 through 9.

Getting this wrong is extremely common and worth practicing until it's automatic — it's one of the most frequent sources of off-by-one bugs in real code.

"How would you print the numbers 1 through 10 inclusive?" (Answer: range(1, 11).)

3 What's the accumulator pattern, and can you give an example?

A variable initialized before the loop (often to 0 or an empty value) that gets updated on every iteration to build up a final result — a running sum, a running count, a running product. Example: total = 0 then total += i inside a loop to sum a range of numbers.

Forgetting to initialize the accumulator before the loop starts — a very common real bug that causes a NameError or wrong starting value.

"What would happen if you initialized a sum accumulator to 1 instead of 0?" (Answer: the final sum would be off by exactly 1, too high.)

4 What's the difference between break and continue?

break exits the loop entirely — no more iterations happen at all. continue skips the rest of the current iteration's code and jumps back to re-check the loop condition, but the loop keeps going.

Confusing which one stops the loop completely versus which one just skips ahead.

"How would you find the first negative number in a list and stop searching once found?" (Expect a loop with an if check and a break once the match is found.)

5 How do you extract each digit of a number using a loop?

Repeatedly use n % 10 to get the last digit, then n = n // 10 to remove it, looping while n > 0. This is the standard pattern behind digit-sum, digit-reversal, and palindrome-check problems.

Trying to convert the number to a string and slice it character by character without realizing the interviewer usually wants the arithmetic approach to test understanding of %/// (though mentioning the string approach as an alternative shows good breadth).

"Modify this to check if a number is a palindrome." (Expect: build the reversed number digit by digit, compare to the original.)

Rapid-Fire Review

Quick recall check — fill in each blank and hit Check.

🎉 Perfect score — you're ready to talk about this chapter in an interview.
← Practice Bank Build the Project →