Iterative Loops Interview Questions and Answers: Output, Complexity and Code

Learn a repeatable way to trace loop values, count exact work and derive tight bounds. Includes nested-loop sums, control-flow output and ten interview answers.

KnowledgeGate Team

Exam prep & CS education

Updated 13 Nov 20266 min read
Iterative Loops Interview Questions and Answers: Output, Complexity and Code

A loop looks simple until the update changes, an inner bound starts depending on the outer variable, or continue and breakThe worked traces span additive and multiplicative loops, dependent and fixed nested bounds, control flow and interview model answers.

Iterative loops in a 60-second interview answer

Identify the control variable and list its exact values. Count valid entries. Check whether inner work is fixed or depends on the outer value, write the sum, then simplify to a tight bound. Exact count and growth class are separate answers.

The update gives the first clue: i += c is linear, while i *= c and i /= c are logarithmic when c > 1. A dependent nested loop needs a sum, not multiplication of its largest bounds.

Service-company screening depth and product-company deep-dive depth are useful labels for answer depth, not rules. Screening answers need traces, boundaries and basic complexity; deeper answers add proofs, mutations, overflow risks and tests. Iterative-loop interviews centre on traces, state transitions and exact sums. Broader algorithm-analysis work handles input-size definitions, tight bounds and recurrence conversions. Algorithm Analysis Interview Questions and Answers covers that wider frame, while DSA using Java structures both.

Trace additive, growing and shrinking loops before naming complexity

Write body-entry values first. They expose inclusive boundaries and the exact execution count.

loop

body-entry values

exact executions

tight growth

for (i=5; i<=29; i+=4)

5, 9, 13, 17, 21, 25, 29

floor((29-5)/4)+1 = 7

Theta(n) when the upper limit is input

for (i=1; i<=n; i*=3)

for n=243: 1, 3, 9, 27, 81, 243

floor(log3 n)+1; for n=243: 6

Theta(log n)

for (i=n; i>=1; i/=2)

for n=64: 64, 32, 16, 8, 4, 2, 1

floor(log2 n)+1; for n=64: 7

Theta(log n)

Replacing <= with < removes the last value when the loop reaches that boundary exactly. Changing the start changes the exact count, even when the asymptotic class stays unchanged.

State the numeric model you assume. Multiplication and division loops need positive starting values and a factor greater than one. Here division is integer division, so it truncates. In every language, the update must still make progress and guarantee termination.

A dependent nested loop does 62 operations, not 160

Define one operation as one execution of count++:

Code
count = 0
for i = 2; i <= n; i = i * 2
    for j = 0; j < i; j = j + 1
        count++

At n=32, the outer values are 2, 4, 8, 16, 32. The inner loop therefore contributes 2, 4, 8, 16, 32 operations, giving 2+4+8+16+32 = 62.

For n=2^m, the sum is 2^1+2^2+...+2^m = 2^(m+1)-2 = 2n-2, so the tight bound is Theta(n), not Theta(n log n). For arbitrary n>=2, the exact count is 2^(floor(log2 n)+1)-2, which is still Theta(n).

Change only the inner condition to j<n. Each of the five outer iterations at n=32 now performs 32 operations, so the total is 5x32=160. For powers of two, that is n log2 n. The condition j<i forms a geometric sum; j<n forms a rectangle.

Two panels compare dependent and fixed inner bounds at n=32. Geometric bars total 62 for j<i, while five equal bars total 160 for j<n.

Two snippets can both total 21 and still have different growth

For Snippet A, the outer loop is i=1..n and the inner loop is j=1..i. At n=6, the trace is triangular:

outer value

inner executions

running total

1

1

1

2

2

3

3

3

6

4

4

10

5

5

15

6

6

21

Thus 1+2+3+4+5+6 = 21, and the general formula n(n+1)/2 gives Theta(n^2).

For Snippet B, the inner update halves j: for (j=i; j>=1; j=floor(j/2)). At n=8:

outer value

inner executions

running total

1

1

1

2

2

3

3

2

5

4

3

8

5

3

11

6

3

14

7

3

17

8

4

21

Its per-value count is floor(log2 i)+1. Summing that expression for i=1..n gives Theta(n log n). One observed total cannot identify a growth class; the formula must show how work changes with n. See Time Complexity: Big-O, Theta, Omega & Master Theorem after completing both derivations.

Two panels show 21 operations from a triangular n=6 loop with Theta(n^2) and a halving inner loop with Theta(n log n).

Trace continue and break as state changes, not guesses

Trace each body action in order:

Code
total = 0
for i = 0; i < 5; i++
    if i == 2
        continue
    total = total + (2*i + 1)
    if total > 10
        break
print(total)

i

continue?

value added

total

break?

0

no

1

1

no

1

no

3

4

no

2

yes

none

4

not checked

3

no

7

11

yes

4

never entered

none

11

not checked

The printed value is 11. continue skips the remaining body, but the for update still runs. break exits immediately. Since the bound is the literal 5, this snippet is Theta(1). For a search bounded by input n, an early break may give best-case Theta(1), while a no-match run remains worst-case Theta(n).

Five screening-depth loop questions with model answers

question

model answer

number or reason to say aloud

1. How do you analyse an unfamiliar loop?

Identify the variable, list its values, count valid entries, inspect inner dependency, write the sum and derive a tight bound.

Exact count and asymptotic growth answer different questions.

2. How many times does i=5; i<=29; i+=4 run?

Seven times.

The values are 5,9,13,17,21,25,29; floor((29-5)/4)+1 = 7.

3. Why is repeated multiplication logarithmic?

After k updates, the value is start x factor^k; solve the boundary for k.

From 1, the six entries 1,3,9,27,81,243 reach 243.

4. What is the count for the dependent nest at n=32?

It is 62, not 160.

2+4+8+16+32 = 62.

5. What does the continue-and-break snippet print?

It prints 11.

Skip the addition at i=2; adding 7 at i=3 makes 11, then the loop breaks.

Five deep-dive follow-ups, traps and tests

question

model answer

number or reason to say aloud

6. What changes if the main outer update becomes i=i+2?

2+4+...+32 = 2(1+...+16) = 2x136 = 272; generally, Theta(n^2).

Evens 2 through 32.

7. Does the same measured count imply the same complexity?

No equal bound can be inferred. Both total 21 here, but n(n+1)/2 is Theta(n^2) and sum(floor(log2 i)+1) is Theta(n log n).

One count proves neither bound.

8. Can i=i*2 overflow and stop making progress?

Yes. Use a wider type or guard the final multiplication.

Overflow can defeat termination.

9. Which boundary tests expose mistakes in the main loop?

n=1 -> 0, n=2 -> 2, n=31 -> 30, n=32 -> 62, n=33 -> 62.

Test around a power of two.

10. Does timing one input prove the complexity?

No. The operation-count formula proves it.

Timing includes machine, compiler and input effects.

KnowledgeGate has over 60 practice questions on Iterative Loops & Code, not frequency evidence. Use Loops & Iteration MCQs in C: 12 Solved PYQs and Coding & DSA Courses for Placements.

Iterative loops interview answers in the short version

Keep one recall chain: trace values -> count entries -> identify dependency -> write the sum -> simplify -> test boundaries. The dependent nest totals 62 at n=32; its fixed-n inner-bound variant totals 160; the control-flow snippet prints 11.

Reproduce the traces without running the code. Then implement the three update families and test the five boundary cases. Coding for Placements offers a broader multi-language practice route.