What is the simplest way to reverse a string in TypeScript using built-in methods?
Split the string into an array of characters, reverse the array, then join it back into a string.
Example: str.split('').reverse().join('')
Click to reveal answer
intermediate
How does the two-pointer approach work for reversing a string?
Use two pointers: one at the start and one at the end of the string (or array). Swap characters at these pointers and move them towards the center until they meet.
Click to reveal answer
beginner
Why can't strings be reversed by swapping characters directly in TypeScript?
Strings are immutable in TypeScript, so you cannot change characters directly. You must convert the string to an array, modify it, then convert back.
Click to reveal answer
advanced
What is a recursive approach to reverse a string?
Recursively call the function with the substring excluding the first character, then append the first character at the end until the string is empty.
Click to reveal answer
intermediate
What is the time complexity of reversing a string using built-in methods in TypeScript?
The time complexity is O(n), where n is the length of the string, because each character is processed once during split, reverse, and join.
Click to reveal answer
Which TypeScript method converts a string into an array of characters?
Asplit('')
Breverse()
Cjoin('')
Dslice()
✗ Incorrect
The split('') method splits a string into an array of individual characters.
Why do we convert a string to an array to reverse it in TypeScript?
ABecause arrays are immutable
BBecause strings are immutable
CBecause strings have no length
DBecause arrays are faster
✗ Incorrect
Strings cannot be changed directly in TypeScript, so we convert to an array to modify characters.
In the two-pointer approach, what do the pointers do?
ASkip characters in the middle
BOnly move from start to end
COnly move from end to start
DSwap characters from start and end moving inward
✗ Incorrect
Two pointers swap characters from the start and end, moving towards the center.
What is the base case in a recursive string reversal?
AWhen the string length is even
BWhen the string length is odd
CWhen the string is empty or has one character
DWhen the string contains spaces
✗ Incorrect
The recursion stops when the string is empty or has one character, as it is already reversed.
What is the time complexity of reversing a string using the two-pointer approach?
AO(n)
BO(n^2)
CO(log n)
DO(1)
✗ Incorrect
Each character is swapped once, so the time complexity is linear, O(n).
Explain three different ways to reverse a string in TypeScript.
Think about built-in methods, manual swapping, and recursive logic.
You got /3 concepts.
Why do we need to convert a string to an array to reverse it in TypeScript? What does immutability mean here?
Consider how strings and arrays differ in mutability.
You got /3 concepts.
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
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.
Step 2: Effect on string
For example, "hello".split('') becomes ["h", "e", "l", "l", "o"].
Final Answer:
It converts the string into an array of individual characters. -> Option B
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
Step 1: Split string into array
Use str.split('') to convert string into array of characters.
Step 2: Reverse and join
Use reverse() on array, then join('') to form reversed string.
Final Answer:
str.split('').reverse().join('') -> Option A
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?