DSA interview question
A leaderboard shows the top score and, right below it, the runner-up score, which must be strictly less than the top score (if two players tie for first, the runner-up is the next distinct score). If every player has the same score, there's no runner-up, and the screen shows "-1". Sorting all the scores just to read two values is wasteful; the classic interview answer finds both in a single pass by tracking the largest and second-largest so far.
Example: [12, 35, 1, 10, 34, 1] → 34.
int secondLargest(const vector<int>& a); // largest value strictly below the maximum, or -1 if none
Input a | Output | Explanation |
|---|---|---|
[12, 35, 1, 10, 34, 1] | 34 | |
[10, 5, 10] | 5 | The duplicate maximum doesn't count as second |
[10, 10, 10] | -1 | No value below the maximum |
[7] | -1 | Only one element |
[1, 2] | 1 | |
[2, 1] | 1 | Order doesn't matter |
(Confirm: values are positive (as in the GeeksforGeeks version), so -1 unambiguously means "no second largest".)
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