ProgrammingHard
A loop calls strlen(s) on a fixed string of length 10^4 once per character of the same string. How many characters are read in total?
- A. About 10^4
- B. About 5 x 10^7
- C. About 10^8
- D. About 2 x 10^4
Correct answer: B. About 5 x 10^7
The i-th call reads about i characters, so the sum is about n^2 / 2 = 5 x 10^7.
More C Programming questions
- A function computes the sum of 1 to n by recursion with depth n. For n = 10^6, what is the main risk?
- A program increments a counter inside three nested loops, each running n = 100 times. How many increments occur?
- In a loop for (i = 0; i < n; i++) x = x 2 % m; with m = 1000000007 and x kept as long long, what is the cost?
- Which loop computes the number of trailing zeros of n in O(number of zeros) time for an unsigned n greater than zero?
- A loop with i starting at 1 and i = 2 until i > n runs how many times for n = 1000?
- A program uses a while loop that finds the smallest power of two at least n for n = 1000, starting from 1. What value does it return?