Challenge - 5 Problems
String Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of String Concatenation and Size
What is the output of the following C++ code snippet?
DSA C++
std::string s1 = "Hello"; std::string s2 = "World"; std::string s3 = s1 + " " + s2; std::cout << s3 << " " << s3.size();
Attempts:
2 left
💡 Hint
Remember that adding a space " " between s1 and s2 increases the length by 1.
✗ Incorrect
The string s3 is formed by concatenating s1, a space, and s2. So s3 is "Hello World" which has 11 characters including the space.
🧠 Conceptual
intermediateUnderstanding String Memory Layout
Which statement correctly describes how a std::string stores its characters in memory?
Attempts:
2 left
💡 Hint
Think about how std::string differs from C-style strings.
✗ Incorrect
std::string stores characters contiguously but does not guarantee a null terminator. It manages size internally and may or may not have a null terminator for compatibility.
🔧 Debug
advancedIdentify the Error in String Initialization
What error does the following C++ code produce?
DSA C++
char* s = "Hello"; s[0] = 'h';
Attempts:
2 left
💡 Hint
String literals are stored in read-only memory in modern C++.
✗ Incorrect
The pointer s points to a string literal which is stored in read-only memory. Modifying it causes a runtime segmentation fault.
❓ Predict Output
advancedOutput of String Substring and Comparison
What is the output of this C++ code?
DSA C++
std::string s = "datastructures"; std::string sub = s.substr(4, 6); if (sub == "struct") std::cout << "Match"; else std::cout << "No Match";
Attempts:
2 left
💡 Hint
Check the substring starting at index 4 with length 6.
✗ Incorrect
The substring from index 4 with length 6 is "struct", so the comparison is true and prints "Match".
🧠 Conceptual
expertString Copy and Memory Sharing in C++
Which statement about copying std::string objects is true in modern C++ implementations?
Attempts:
2 left
💡 Hint
Modern C++ std::string implementations do not use copy-on-write.
✗ Incorrect
Modern C++ std::string implementations create a new copy of the characters when copied to avoid sharing mutable state.
