Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

String Reversal Approaches in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
String Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of String Reversal Using Array Methods
What is the output of the following TypeScript code that reverses a string using array methods?
DSA Typescript
const str = "hello";
const reversed = str.split('').reverse().join('');
console.log(reversed);
A"hello"
B"olleh"
C"ohlle"
D"helol"
Attempts:
2 left
💡 Hint
Think about what split, reverse, and join do to the string.
❓ Predict Output
intermediate
2:00remaining
Output of Manual String Reversal Using Loop
What is the output of this TypeScript code that reverses a string manually using a loop?
DSA Typescript
const str = "abcde";
let reversed = "";
for(let i = str.length - 1; i >= 0; i--) {
  reversed += str[i];
}
console.log(reversed);
A"edcba"
B"abcde"
C"edcab"
D"abc"
Attempts:
2 left
💡 Hint
The loop starts from the end of the string and adds characters to reversed.
🔧 Debug
advanced
2:00remaining
Identify the Error in Recursive String Reversal
What error does this TypeScript recursive string reversal function produce when called with "test"?
DSA Typescript
function reverseString(s: string): string {
  if (s.length === 0) return s;
  return reverseString(s.substring(1)) + s[0];
}
console.log(reverseString("test"));
A"tset"
BStack overflow error
CTypeError: s.substring is not a function
D"test"
Attempts:
2 left
💡 Hint
Check the base case and how substring is used.
❓ Predict Output
advanced
2:00remaining
Output of String Reversal Using Spread Operator and Reduce
What is the output of this TypeScript code that reverses a string using the spread operator and reduce?
DSA Typescript
const str = "world";
const reversed = [...str].reduce((acc, char) => char + acc, '');
console.log(reversed);
A"world"
B"wrold"
C"dlrow"
D"dlorw"
Attempts:
2 left
💡 Hint
Reduce accumulates characters by adding current char before accumulator.
🧠 Conceptual
expert
2:00remaining
Time Complexity of Different String Reversal Methods
Which option correctly describes the time complexity of reversing a string of length n using a manual loop versus using built-in methods like split, reverse, and join?
ABoth manual loop and built-in methods have O(n) time complexity.
BBoth manual loop and built-in methods have O(1) time complexity.
CManual loop is O(n); built-in methods are O(n^2) because of array operations.
DManual loop is O(n^2) due to string concatenation; built-in methods are O(n).
Attempts:
2 left
💡 Hint
Consider how string concatenation inside a loop affects performance.

Practice

(1/5)
1. What does the split('') method do when applied to a string in TypeScript?
easy
A. It reverses the string directly.
B. It converts the string into an array of individual characters.
C. It joins the string elements into one string.
D. It removes all spaces from the string.

Solution

  1. Step 1: Understand the split method

    The split('') method breaks a string into an array of characters by splitting at every empty string between characters.
  2. Step 2: Effect on string

    For example, "hello".split('') becomes ["h", "e", "l", "l", "o"].
  3. Final Answer:

    It converts the string into an array of individual characters. -> Option B
  4. Quick Check:

    split('') = array of chars [OK]
Hint: split('') breaks string into chars array [OK]
Common Mistakes:
  • Thinking split('') reverses the string
  • Confusing split('') with join()
  • Assuming split('') removes spaces
2. Which of the following is the correct syntax to reverse a string str using built-in methods in TypeScript?
easy
A. str.split('').reverse().join('')
B. str.reverse().join('')
C. reverse(str).split('')
D. str.join('').reverse()

Solution

  1. Step 1: Split string into array

    Use str.split('') to convert string into array of characters.
  2. Step 2: Reverse and join

    Use reverse() on array, then join('') to form reversed string.
  3. Final Answer:

    str.split('').reverse().join('') -> Option A
  4. Quick Check:

    split + reverse + join = reversed string [OK]
Hint: Split, reverse array, then join to reverse string [OK]
Common Mistakes:
  • Calling reverse() directly on string
  • Using join() before reverse()
  • Using reverse() as a standalone function
3. What will be the output of the following TypeScript code?
const str = "abcde";
const reversed = str.split('').reverse().join('');
console.log(reversed);
medium
A. "edcbe"
B. "abcde"
C. ""
D. "edcba"

Solution

  1. Step 1: Split string into array

    "abcde".split('') becomes ["a", "b", "c", "d", "e"].
  2. Step 2: Reverse and join array

    Reversing gives ["e", "d", "c", "b", "a"], joining results in "edcba".
  3. Final Answer:

    "edcba" -> Option D
  4. Quick Check:

    split + reverse + join = "edcba" [OK]
Hint: Split, reverse, join outputs reversed string [OK]
Common Mistakes:
  • Expecting original string output
  • Mixing up reversed characters
  • Forgetting to join after reverse
4. Identify the error in this TypeScript function intended to reverse a string:
function reverseString(s: string): string {
  return s.reverse();
}
medium
A. Strings do not have a reverse() method directly.
B. The function should return void, not string.
C. The parameter should be an array, not a string.
D. The function is missing a return statement.

Solution

  1. Step 1: Check string methods

    Strings in TypeScript do not have a reverse() method directly; only arrays do.
  2. Step 2: Correct approach

    Convert string to array with split(''), reverse array, then join back.
  3. Final Answer:

    Strings do not have a reverse() method directly. -> Option A
  4. Quick Check:

    reverse() works on arrays, not strings [OK]
Hint: reverse() is for arrays, not strings [OK]
Common Mistakes:
  • Trying to call reverse() on string directly
  • Ignoring need to split string first
  • Assuming reverse() returns void
5. Given the function below, what will be the output when calling reverseString('12345')?
function reverseString(s: string): string {
  let reversed = '';
  for (let i = s.length - 1; i >= 0; i--) {
    reversed += s[i];
  }
  return reversed;
}
hard
A. "5432"
B. "12345"
C. "54321"
D. ""

Solution

  1. Step 1: Loop from end to start

    The loop starts at index 4 (last char '5') and moves backward to 0.
  2. Step 2: Build reversed string

    Characters are appended in reverse order: '5', '4', '3', '2', '1'.
  3. Final Answer:

    "54321" -> Option C
  4. Quick Check:

    Loop backward concatenates reversed string [OK]
Hint: Loop backward to build reversed string [OK]
Common Mistakes:
  • Looping forward instead of backward
  • Off-by-one errors in loop index
  • Not initializing reversed string properly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard