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?- A. eivretni* 9
- B. weivretni* 10
- C. *weivretni 10
- 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
- Problem: Find the longest substring in which every character appears at least k times (divide and conquer on characters that are too…
- Problem: Return the minimum number of characters to insert to make a string a palindrome. python def mininsert(s): t = s[::-1]; n = len(s)…
- Problem: Count the number of islands after each 'add land' operation on an initially all-water grid. python def islandsonline(R, C, ops)…
- Problem: Return the length of the shortest path that visits every node of a small undirected graph (start and end anywhere, edges…
- Problem: Count the number of ways to assign n distinct tasks to n workers where task i cannot go to worker i (derangements), modulo 10⁹ +…
- Problem: Reconstruct a queue of people (h, k) where k = number of people in front with height ≥ h. python def rebuild(people)…