KatInterview

Peak Average Score: Overflow, Large Files, and Java Concurrency

Arrays & HashingModel answer
MediumModel answerArrays & Hashingjavaconcurrencyaggregation

Last updated

Aggregate student scores to find the highest per-student average, then extend the solution for huge files, negative values, integer overflow, and multithreaded processing.

You receive a list of exam records. Each record contains a student's name and one integer score they received, represented as a decimal string. Write a method that returns the highest average score any student has earned, truncated down to a whole number.

public static int highestAverage(String[][] records)

Each element is a two-entry array {name, score}:

{{"Bob", "87"}, {"Mike", "35"}, {"Bob", "52"}, {"Jason", "35"}, {"Mike", "55"}, {"Jessica", "99"}}

Bob's scores sum to 139 over 2 entries (average 69.5), Mike's sum to 90 over 2 (average 45), Jason has one score of 35, and Jessica has one score of 99. Return 99.

Write the implementation on paper — no IDE or compiler. Each part below asks you to evolve your previous code rather than describe a solution in the abstract.

Before writing anything, ask yourself:

  • What should be returned when the input is empty?
  • Should a malformed row (wrong column count, non-numeric score) be skipped or cause an immediate failure?
  • Are names case-sensitive? Should surrounding whitespace be stripped?
  • What range can a score take? Is there a limit on how many scores one student can accumulate?

Part 1: All Records Fit in Memory

Implement highestAverage for an input small enough to load completely into memory.

Your solution should:

  • Make a single pass and store only summary data per student, not every individual score
  • Use the correct rounding behavior when computing integer averages
  • Be analyzed for time and space complexity in terms of total records and distinct students

Part 2: The File Is Too Large for RAM

The same records now arrive from a log file that is many times larger than available heap space. Adapt your solution.

Address both sub-cases:

  • The number of distinct students is small enough that a summary map fits in memory, but the raw record count is enormous
  • The number of distinct students is itself too large to fit in memory at once

Cover the I/O cost of your approach and explain why partial aggregates can be combined correctly.

Part 3: Negative Scores and Integer Overflow

The problem changes in two ways: scores may be negative, and a single student's cumulative score total may exceed the range of a Java int.

Identify and fix:

  • Where rounding behavior in your original code silently gives the wrong answer for negative averages
  • Any initial values that implicitly assumed non-negative scores
  • The safest type choice for accumulated sums, and how to handle inputs that would overflow even that type

Explain whether floating-point arithmetic is a suitable replacement and why.

Part 4: Multithreading, Atomics, and Locks

Rewrite the in-memory solution to distribute work across multiple threads.

Then answer the following without looking anything up:

  1. What does "atomic" mean for an operation? What guarantee do Java's classes in java.util.concurrent.atomic (for example, AtomicLong) provide that a plain long field does not?
  2. What are the behavioral differences between synchronized and ReentrantLock? When would you reach for one over the other?
  3. How does your design ensure that the thread collecting final results sees every write made by the worker threads?

Hints

Hint 1

For the out-of-memory case, separate the two distinct problems: too many raw records versus too many distinct students. Each has its own solution, and only the second one requires repartitioning the data on disk.

Hint 2

Java integer division truncates toward zero, not toward negative infinity. For a negative quotient such as -7 / 2, those two behaviors give different results. Check whether your code actually needs Math.floorDiv.

Hint 3

Keep worker threads entirely free of shared mutable state during the processing phase. Each thread builds its own local map, and the main thread merges after all workers have finished. This sidesteps locking during the hot path entirely.