DSA interview question
A configuration loader reads numbers typed by users, such as " -042", "1337px" and "99999999999", and must turn them into 32-bit integers the way C's atoi does: skip leading spaces, read one optional sign, read digits until the first non-digit, and clamp anything outside the 32-bit range. The interviewer wants it written recursively: each call handles one character and passes the value built so far to the next call, so the "loop" becomes the call chain.
Example: " -042" → -42; "-91283472332" → -2147483648.
int myAtoi(const string& s);
| Input | Output | Explanation |
|---|---|---|
"42" | 42 | |
" -042" | -42 | Spaces, a sign and leading zeros |
"1337c0d3" | 1337 | Stops at the first non-digit |
"words and 987" | 0 | No digits before the first letter |
"-91283472332" | -2147483648 | Clamped to INT_MIN |
"+-12" | 0 | Only one sign is allowed |
(Confirm: only the space character counts as whitespace; no digits means 0; results are clamped to [−2³¹, 2³¹ − 1].)
myAtoi(string s), which converts a string to a 32-bit signed integer" (LeetCode 8), with the extra requirement "do it recursively".' ' in LeetCode's version.)On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in