Bird
Raised Fist0
Pythonprogramming~5 mins

While loop execution flow in Python - Time & Space Complexity

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Time Complexity: While loop execution flow
O(n)
Understanding Time Complexity

We want to understand how the time a while loop takes changes as the input grows.

Specifically, how many times the loop runs affects the total work done.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


count = 0
while count < n:
    print(count)
    count += 1

This code prints numbers from 0 up to n-1 using a while loop.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: The while loop runs repeatedly.
  • How many times: It runs once for each number from 0 to n-1, so n times.
How Execution Grows With Input

Each time n grows, the loop runs more times, directly increasing work.

Input Size (n)Approx. Operations
1010 times
100100 times
10001000 times

Pattern observation: The number of operations grows in a straight line with n.

Final Time Complexity

Time Complexity: O(n)

This means the time taken grows directly in proportion to the input size.

Common Mistake

[X] Wrong: "The while loop runs forever or a fixed number of times regardless of n."

[OK] Correct: The loop stops when count reaches n, so it depends exactly on n, not fixed or infinite.

Interview Connect

Understanding how loops grow with input size is a key skill that helps you explain your code clearly and think about efficiency.

Self-Check

"What if we increased count by 2 each time instead of 1? How would the time complexity change?"

Practice

(1/5)
1.

What does a while loop do in Python?

easy
A. Stops the program immediately
B. Runs code only once
C. Repeats code as long as a condition is true
D. Runs code a fixed number of times

Solution

  1. Step 1: Understand the purpose of a while loop

    A while loop runs repeatedly while its condition is true.
  2. Step 2: Compare options with this behavior

    Only Repeats code as long as a condition is true describes repeating code while a condition is true.
  3. Final Answer:

    Repeats code as long as a condition is true -> Option C
  4. Quick Check:

    While loop = repeat while true [OK]
Hint: While loops run repeatedly while condition is true [OK]
Common Mistakes:
  • Thinking while loops run a fixed number of times
  • Confusing while with if statement
  • Believing while loops run only once
2.

Which of the following is the correct syntax to start a while loop in Python?

?
easy
A. while x > 0:
B. while (x > 0)
C. while x > 0 {}
D. while x > 0 then

Solution

  1. Step 1: Recall Python while loop syntax

    Python uses a colon after the condition and no parentheses or braces.
  2. Step 2: Match options with correct syntax

    while x > 0: uses 'while x > 0:' which is correct syntax.
  3. Final Answer:

    while x > 0: -> Option A
  4. Quick Check:

    Colon ends while condition line [OK]
Hint: Use colon after condition, no braces or 'then' [OK]
Common Mistakes:
  • Adding braces {} like other languages
  • Using 'then' keyword
  • Putting condition in parentheses unnecessarily
3.

What will be the output of this code?

count = 3
while count > 0:
    print(count)
    count -= 1
print("Done")
medium
A. Done
B. 3 2 1 Done
C. 3 2 1 0 Done
D. 3 2 1 2 Done

Solution

  1. Step 1: Trace the while loop iterations

    count starts at 3, prints 3, then decreases to 2, prints 2, then 1, then stops when count is 0.
  2. Step 2: Note the final print after loop

    After loop ends, "Done" is printed.
  3. Final Answer:

    3 2 1 Done -> Option B
  4. Quick Check:

    Prints 3,2,1 then Done [OK]
Hint: Count decreases before loop ends, stops at zero [OK]
Common Mistakes:
  • Expecting 0 to print inside loop
  • Forgetting to decrease count
  • Thinking loop runs forever
4.

Find the error in this code snippet:

i = 1
while i < 5
    print(i)
    i += 1
medium
A. Missing colon after while condition
B. Variable i is not initialized
C. Indentation error in print statement
D. Infinite loop because i never changes

Solution

  1. Step 1: Check while loop syntax

    The while line must end with a colon (:).
  2. Step 2: Identify the missing colon

    The code misses ':' after 'while i < 5', causing syntax error.
  3. Final Answer:

    Missing colon after while condition -> Option A
  4. Quick Check:

    While line needs colon [OK]
Hint: Always put colon after while condition [OK]
Common Mistakes:
  • Forgetting colon after while
  • Misindenting inside loop
  • Not updating loop variable
5.

Consider this code:

n = 5
result = 1
while n > 1:
    result *= n
    n -= 1
print(result)

What does this code calculate?

hard
A. Sum of numbers from 1 to 5
B. Infinite loop with no output
C. Product of numbers from 1 to 4
D. Factorial of 5 (5!)

Solution

  1. Step 1: Understand the loop's multiplication

    result multiplies by n each time, starting at 5 down to 2.
  2. Step 2: Recognize factorial pattern

    Multiplying 5*4*3*2*1 equals 5 factorial (5!).
  3. Final Answer:

    Factorial of 5 (5!) -> Option D
  4. Quick Check:

    Multiplying down to 1 = factorial [OK]
Hint: Multiplying down from n to 1 calculates factorial [OK]
Common Mistakes:
  • Thinking it sums numbers instead of multiplies
  • Stopping multiplication at 2 instead of 1
  • Assuming infinite loop