DSA interview question
A lottery system stores huge ticket numbers as digit strings, far too long for any integer type. A promotion wants, for each ticket, the largest odd number that appears as a contiguous substring of it (or an empty string if there's none). It sounds like it needs every substring, but a number is odd exactly when its last digit is odd, and a longer prefix is a larger number. So the answer is just the prefix ending at the rightmost odd digit, found in one backwards scan.
Example: "35427" → "35427"; "4206" → ""; "10133890" → "1013389".
string largestOddNumber(const string& num); // digits only; leading zeros of the answer are stripped
Input num | Output | Explanation |
|---|---|---|
"52" | "5" | The rightmost odd digit is 5 |
"4206" | "" | Every digit is even |
"35427" | "35427" | It already ends in an odd digit |
"10133890" | "1013389" | The rightmost odd digit is the 9 |
"7" | "7" | |
"0214638" | "21463" | Prefix "021463", with the leading zero stripped |
(Confirm: the input contains only digits; leading zeros in the answer are removed, which LeetCode's inputs never have anyway.)
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