ProgrammingHard
For a number n up to 10^18 read as a decimal string, what is the number of digits, computed with one pass?
- A. O(n), which is about 10^18 steps
- B. O(length of the string), which is at most 19 digits for a value below 10^18 + 1
- C. O(sqrt n), which is about 10^9 steps
- 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
- A program keeps a set of at most 10^5 ints and answers membership queries 10^5 times. Which structure is correct and fast enough?
- Which statement about finding the length of a null-terminated C string is correct?
- Which code gives the correct sum of the first n squares, for n up to 10^6, using a closed form in O(1)?
- Which operation on a dynamic array of n elements has O(1) amortised cost when using doubling?
- A loop does for (i = n; i > 0; i /= 2) for (j = 0; j < i; j++) work();. For n = 1024, how many times is work() called in total?
- Which of these is an O(n) method to test whether a string of length n is a palindrome in C?