DSA interview question
An intern built the leaderboard for a coding contest. It reads the scores of every participant and prints the total, the average and the best score. It worked perfectly in testing with five players. On contest day, with 100,000 players scoring up to 10^9 points each, the total came out negative and the average was a whole number even when it shouldn't be. Nothing crashed; the numbers were just silently wrong. Your task: rewrite the summary so it is correct for the real limits, and be able to explain which data type each value needs and why.
Example: scores [1, 2, 3, 4] → Total 10, Average 2.50, Max 4.
long long totalScore(const vector<int>& scores); // sum of all scores
double averageScore(const vector<int>& scores); // real-valued mean
int maxScore(const vector<int>& scores); // highest score
Input scores | Output | Explanation |
|---|---|---|
[10, 20, 30] | Total 60, Average 20.00, Max 30 | Simple case |
[1, 2, 3, 4] | Total 10, Average 2.50, Max 4 | Integer division would wrongly give 2 |
[1000000000, 1000000000, 1000000000] | Total 3000000000, Average 1000000000.00, Max 1000000000 | Total exceeds INT_MAX (2147483647), so an int sum overflows |
[7] | Total 7, Average 7.00, Max 7 | Single element |
[0, 0] | Total 0, Average 0.00, Max 0 | All zeros |
(Confirm: 1 ≤ N ≤ 10^5 and 0 ≤ score ≤ 10^9, so the total can reach 10^14; average printed with 2 decimals.)
int overflow.int? When would you use long long?"int and long long?"int overflows?"7 / 2 give 3?"long long x = a * b; still overflows even though x is long long. Why?"float or double?"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