Data Structures & AlgorithmsHardGATE CS · TCS NQT · Product Company SDE

Two nested loops iterate: the outer runs n times, and for each outer iteration the inner loop runs from 1 up to the current outer index i. What is the total number of inner-loop iterations?

  1. A. n(n+1)/2
  2. B. n^2
  3. C. n(n-1)/2
  4. D. 2^n

Correct answer: A. n(n+1)/2

Total iterations are 1 + 2 + ... + n = n(n+1)/2, which is still O(n^2) asymptotically.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic