Interview Questions
std::string · Study material
C++ std::string – Interview Questions
1. What is the difference between std::string and a C-style string?
A C string is a null-terminated char array with a fixed buffer and C library functions. std::string is a class that stores its length, manages and grows its memory, and supports copying, comparison and concatenation with operators.
2. What does find() return if the substring is not found?
std::string::npos, the largest value of size_t. Compare with npos, not with -1.
3. What is the difference between size() and capacity() for a string?
size is the number of characters; capacity is how many characters fit before reallocation. Short strings often fit inside the object itself (small string optimisation).
4. What does substr(pos, len) do?
Returns a new string with up to len characters starting at pos. It throws std::out_of_range if pos > size().
5. How do you convert an int to a string and back?
std::to_string(n) and std::stoi(s); also std::format("{}", n) and std::from_chars / std::to_chars for fast, locale-independent conversion.
6. Why does "Hello" + "World" not compile?
Both operands are const char arrays that decay to pointers, and pointers cannot be added. At least one operand must be a std::string: std::string("Hello") + "World" or "Hello"s + "World".
7. What is std::string_view?
A C++17 non-owning reference to a sequence of characters (pointer + length). It is cheap to copy, avoids allocations, but must not outlive the data it refers to.
8. How are strings compared?
Lexicographically, character by character using char values; a shorter string that is a prefix of a longer one is smaller.
9. What is the small string optimisation (SSO)?
Implementations store short strings (typically up to 15–22 characters) inside the string object without heap allocation, making them very cheap.
10. How do you check if a string starts with a prefix?
s.starts_with("pre") in C++20, or s.rfind("pre", 0) == 0 in older code.
11. What does c_str() return?
A const char* to the string's characters followed by a null terminator, valid until the string is modified or destroyed.
12. What happens with s.at(i) when i is out of range?
It throws std::out_of_range; s[i] does not check and causes undefined behaviour (s[s.size()] is allowed and gives '\0').
13. How do you split a string by a delimiter?
Use std::getline on an std::istringstream with the delimiter, or loop with find and substr. C++20 ranges offer std::views::split.
14. How do you remove all spaces from a string?
std::erase(s, ' ') in C++20, or s.erase(std::remove(s.begin(), s.end(), ' '), s.end()).
15. Is std::string mutable?
Yes – unlike strings in Java or Python, a C++ string's characters can be changed in place.
16. How do you check whether two strings are anagrams?
Sort copies of both and compare, or count character frequencies in an array of 26 or 256 ints.
17. What exceptions can std::stoi throw?
std::invalid_argument when no conversion can be performed, and std::out_of_range when the value does not fit in an int.
18. How do you reverse a string?
std::reverse(s.begin(), s.end()) in place, or std::string r(s.rbegin(), s.rend()) for a reversed copy.
19. What is the "s" suffix in "abc"s?
A user-defined literal from std::string_literals that creates a std::string instead of a const char array.
20. What is the complexity of s.find(t)?
O(n × m) in the worst case for the usual implementation; for many searches on large texts, algorithms such as std::boyer_moore_searcher are faster.