ProgrammingHard

For a number n up to 10^18 read as a decimal string, what is the number of digits, computed with one pass?

  1. A. O(n), which is about 10^18 steps
  2. B. O(length of the string), which is at most 19 digits for a value below 10^18 + 1
  3. C. O(sqrt n), which is about 10^9 steps
  4. D. O(log log n) by a table lookup

Correct answer: B. O(length of the string), which is at most 19 digits for a value below 10^18 + 1

A digit count comes from the string length in one pass. The value itself is not iterated over, so the cost tracks the number of characters.

More C Programming questions

Exams that ask this topic