Skip to main contentSkip to solution

Solution

✅ Correct Option: 3

In a nested loop, the inner loop runs completely for every single iteration of the outer loop. If both loops run nn times, the total number of executions is n×n=n2n \times n = \boxed{n^2}.

For example, for i in 1..n { for j in 1..n { ... } } executes the inner body n2n^2 times, so the time complexity is O(n2)O(n^2).


Option 2: nn is the complexity of a single loop, not a loop within a loop.

Note that this holds when both loops depend on nn. If the inner loop runs a fixed number of times, the result is only O(n)O(n).

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question