Data Structures & AlgorithmsHardCodeforces Div2 C · recursion

Problem: A string of length n is built using the operation 'append the reverse of the current string'. Starting with 'a', the length after k operations is 2^k. What is the complexity of an O(1)-per-character check of the k-th character by recursion on halves?

  1. A. O(n)
  2. B. O(n log n)
  3. C. O(1) per query with no structure
  4. D. O(log n)

Correct answer: D. O(log n)

Each recursion step halves the index range, so a query touches O(log n) levels. Generating the whole string takes O(n) and is the trap for large k.

More Coding Interview Problems questions

Exams that ask this topic