Challenge - 5 Problems
String Structure Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of String Modification in C++
What is the output of this C++ code that modifies a string character?
DSA C++
std::string s = "hello"; s[1] = 'a'; std::cout << s << std::endl;
Attempts:
2 left
💡 Hint
Remember strings in C++ are mutable sequences of characters.
✗ Incorrect
The code changes the second character 'e' to 'a', so the string becomes "hallo".
🧠 Conceptual
intermediateWhy Strings Are Considered Data Structures
Which of the following best explains why strings are considered data structures and not just plain text?
Attempts:
2 left
💡 Hint
Think about how strings organize data internally.
✗ Incorrect
Strings organize characters in a sequence and support operations like indexing, slicing, and concatenation, which are typical of data structures.
❓ Predict Output
advancedOutput of String Concatenation and Length in C++
What is the output of this C++ code snippet?
DSA C++
std::string a = "Data"; std::string b = "Structure"; std::string c = a + b; std::cout << c << " " << c.length() << std::endl;
Attempts:
2 left
💡 Hint
Check how concatenation works and count characters carefully.
✗ Incorrect
Concatenating "Data" and "Structure" results in "DataStructure" which has 13 characters.
🔧 Debug
advancedIdentify the Error in String Indexing
What error does this C++ code produce?
DSA C++
std::string s = "example"; char c = s[10]; std::cout << c << std::endl;
Attempts:
2 left
💡 Hint
Consider what happens when accessing an index beyond string length using operator[].
✗ Incorrect
Using operator[] with an out-of-bounds index causes undefined behavior in C++, not a runtime exception.
🚀 Application
expertResulting String After Multiple Operations
Given the following C++ code, what is the final printed string?
DSA C++
std::string s = "abc"; s += "def"; s.replace(2, 4, "XYZ"); s.insert(1, "123"); std::cout << s << std::endl;
Attempts:
2 left
💡 Hint
Apply each operation step-by-step on the string.
✗ Incorrect
Start with "abc", append "def" → "abcdef".
Replace 4 chars from index 2 with "XYZ" → "abXYZ".
Insert "123" at index 1 → "a123bXYZ".
