Learn AP Comp Sci

Problem of the Day

Thursday, May 28, 2026


Problem:

Consider the following code segment, in which the greeting "Hello, world!" is printed a number of times that varies as a function on n.

for (int n = 0; n < 10; n++)
for (int i = 0; i < (Math.pow(2, n)); i++)
System.out.println("Hello, world!");

What is the Big-O performance of this segment?

  1. O(1) - constant
  2. O(n) - linear
  3. O(n log n) - log linear
  4. O(n2) - quadratic
  5. O(2n) - exponential

Show solution:

The correct answer is e. As n increases linearly, the inner i loop runs 2n times, so the number of "Hello, world!" outputs being printed is increasing at an exponential rate.