Challenge - 5 Problems
String Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of character access in a loop
What is the output of the following code snippet?
DSA Python
s = "hello" result = [] for i in range(len(s)): if i % 2 == 0: result.append(s[i]) print(''.join(result))
Attempts:
2 left
💡 Hint
Look at characters at even indexes only.
✗ Incorrect
The loop picks characters at indexes 0, 2, 4 which are 'h', 'l', 'o'. Joining them gives 'hlo'.
❓ Predict Output
intermediate2:00remaining
Output of negative indexing in string
What will be printed by this code?
DSA Python
s = "world" print(s[-1] + s[-2] + s[-5])
Attempts:
2 left
💡 Hint
Negative indexes count from the end starting at -1.
✗ Incorrect
s[-1] is 'd', s[-2] is 'l', s[-5] is 'w'. Concatenated: 'dlw'.
🔧 Debug
advanced2:00remaining
Identify the error in string character access
What error does this code raise when executed?
DSA Python
s = "data" for i in range(len(s)+1): print(s[i])
Attempts:
2 left
💡 Hint
Check the range and valid indexes for string s.
✗ Incorrect
The loop tries to access s[4] which is out of range for string of length 4, causing IndexError.
❓ Predict Output
advanced2:00remaining
Output of slicing with step in string
What is the output of this code?
DSA Python
s = "algorithm" print(s[1:8:2])
Attempts:
2 left
💡 Hint
Slice from index 1 to 7 with step 2.
✗ Incorrect
Indexes 1,3,5,7 correspond to 'l', 'r', 'i', 'h'. Joining gives 'lrih'.
🧠 Conceptual
expert2:00remaining
Count of unique characters accessed in loop
Consider the code below. How many unique characters are printed?
DSA Python
s = "abracadabra" seen = set() for i in range(len(s)): if s[i] not in seen: print(s[i], end='') seen.add(s[i])
Attempts:
2 left
💡 Hint
Count distinct characters in the string.
✗ Incorrect
The unique characters are 'a', 'b', 'r', 'c', 'd'. Counting carefully, there are 5 unique letters but the code prints them as they appear first time. The string has 'a','b','r','c','d' only 5 unique letters, but the code prints them once each, so 5 characters printed.