PPractick

DSA interview question

C++ Basics: Data Types, Ranges and Overflow

Easy · Algorithms · Topics: language basics

The problem

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.

Function signature

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

Test cases

Input scoresOutputExplanation
[10, 20, 30]Total 60, Average 20.00, Max 30Simple case
[1, 2, 3, 4]Total 10, Average 2.50, Max 4Integer division would wrongly give 2
[1000000000, 1000000000, 1000000000]Total 3000000000, Average 1000000000.00, Max 1000000000Total exceeds INT_MAX (2147483647), so an int sum overflows
[7]Total 7, Average 7.00, Max 7Single element
[0, 0]Total 0, Average 0.00, Max 0All zeros

(Confirm: 1 ≤ N ≤ 10^5 and 0 ≤ score ≤ 10^9, so the total can reach 10^14; average printed with 2 decimals.)

How interviewers phrase it

Clarifying questions to ask

  1. What are the maximum values of N and of each element? (Multiply them to get the largest possible total.)
  2. Can values be negative?
  3. Should the average be exact, rounded, or truncated, and to how many decimals?
  4. Can the array be empty? (Division by zero for the average.)

Follow-ups to expect

  1. "What's the range of int and long long?"
  2. "What happens when a signed int overflows?"
  3. "Why does 7 / 2 give 3?"
  4. "long long x = a * b; still overflows even though x is long long. Why?"
  5. "float or double?"
  6. "How big are the basic types?"

Practise questions like this with a real engineer

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 interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in