Challenge - 5 Problems
String Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of character access in a loop
What is the output of the following TypeScript code that traverses a string and prints each character with its index?
DSA Typescript
const str = "hello"; for (let i = 0; i < str.length; i++) { console.log(i + ":" + str[i]); }
Attempts:
2 left
💡 Hint
Remember that str[i] accesses the character at index i, and i is the index number.
✗ Incorrect
The loop runs from 0 to 4 (length of "hello" - 1). Each iteration prints the index and the character at that index separated by a colon.
❓ Predict Output
intermediateOutput of string traversal with conditional character check
What is the output of this TypeScript code that prints only vowels from the string?
DSA Typescript
const text = "typescript"; for (let i = 0; i < text.length; i++) { if ('aeiou'.includes(text[i])) { console.log(text[i]); } }
Attempts:
2 left
💡 Hint
Check which characters in "typescript" are vowels (a, e, i, o, u).
✗ Incorrect
The code checks each character if it is in the string "aeiou". Only 'e' and 'i' appear as vowels in "typescript".
🔧 Debug
advancedIdentify the error in string traversal code
What error does this TypeScript code produce when trying to print each character of a string?
DSA Typescript
const s = "debug"; for (let i = 0; i <= s.length; i++) { console.log(s[i]); }
Attempts:
2 left
💡 Hint
Check the loop condition and what happens when i equals s.length.
✗ Incorrect
The loop runs from 0 to s.length inclusive, so on the last iteration s[s.length] is undefined, which prints as 'undefined'.
🧠 Conceptual
advancedUnderstanding string immutability in traversal
Why can't you change characters of a string directly by index in TypeScript during traversal?
Attempts:
2 left
💡 Hint
Think about whether strings can be changed after creation.
✗ Incorrect
Strings in TypeScript (and JavaScript) are immutable, meaning you cannot change a character by assigning to an index. You must create a new string instead.
🚀 Application
expertFind the first repeated character in a string
Given the string "swiss", what is the first character that repeats when traversing from left to right?
DSA Typescript
const word = "swiss"; const seen = new Set<string>(); let repeated = ''; for (let i = 0; i < word.length; i++) { if (seen.has(word[i])) { repeated = word[i]; break; } seen.add(word[i]); } console.log(repeated);
Attempts:
2 left
💡 Hint
Track characters seen and find the first one that appears again.
✗ Incorrect
The first repeated character in "swiss" is 's' because it appears at index 0 and repeats at index 3.
