Data Structures & AlgorithmsModerateAccenture · Cognizant coding round pattern

Problem: Reverse a word after prepending a marker using Java's StringBuilder.

Problem: Reverse a word after prepending a marker using Java's StringBuilder.
StringBuilder sb = new StringBuilder("interview");
sb.insert(0, '*').reverse().deleteCharAt(0);
System.out.println(sb + " " + sb.length());
What is printed?
  1. A. eivretni* 9
  2. B. weivretni* 10
  3. C. *weivretni 10
  4. D. weivretni 9

Correct answer: A. eivretni* 9

StringBuilder methods mutate in place and return this, so the calls chain on one buffer: "*interview" → reversed "weivretni*" → deleteCharAt(0) removes the FIRST character of the reversed text, 'w', not the marker. Result "eivretni*", length 9. Each operation is O(n); building strings this way avoids the O(n²) cost of repeated String concatenation.

More Coding Interview Problems questions

Exams that ask this topic