← Questions

Maximum value in a file too big for memory

0
GoogleCodingLast asked Dec 2025

A coding interview question asked in Google's first round (coding), last asked Dec 2025. It tests concurrency, streaming and files.

A text file on disk holds one signed 64-bit integer per line, in decimal. It is tens or hundreds of gigabytes, far larger than RAM, and there is no database: only the file system and your language's standard I/O. First find the maximum on one machine, then make it faster with many cores or many machines.

Part 1: one machine

  • max_in_file(path: str) -> int | None — read the file sequentially and return the largest value, or None for an empty file.
file contents:  "12\n-7\n9223372036854775807\n0\n"   ->  9223372036854775807
file contents:  "-5\n-3\n-9\n"                       ->  -3      # don't start the running max at 0
file contents:  ""                                   ->  None

Give pseudo-code, the time and space cost, and how you read the file (line by line, or large buffered blocks).

Part 2: in parallel

Split the work across threads, processes or machines and combine the partial results. Explain:

  • how you cut the file into byte ranges and handle a number that straddles a boundary (each worker skips the partial line at its start, unless it starts at byte 0, and finishes the line that crosses its end);
  • how partial maxima combine (max is associative, so any tree of merges works);
  • when more workers stop helping.

Constraints

  • Values fit in a signed 64-bit integer. Lines end in \n; the last line may or may not have one.
  • Memory is a small constant fraction of the file size.

Clarifying questions

  • Can lines be blank or malformed? Skip, fail, or report them?
  • Is the file on a local SSD, a spinning disk, or a distributed file system?
  • Is an empty file possible, and what should it return?
  • Is this a one-off, or will the query run repeatedly on a growing file?

What a strong answer covers

  • One pass, O(N) time, O(1) memory beyond a read buffer; initialise from the first value, not 0 or a magic constant.
  • Reading in big blocks and parsing bytes yourself beats line-by-line reads when parsing is the bottleneck.
  • The job is usually I/O bound: on one disk, extra threads help only until disk bandwidth is saturated; on a distributed file system, put workers where the blocks are.
  • Correct chunk boundaries, with a test where a number straddles them.
  • Failure handling: a worker dies (re-run its range; the work is idempotent), stragglers, and malformed lines.

Follow-ups

  • Return the top k values instead of the maximum (a min-heap of size k per worker, then merge).
  • Return the median, which does not combine like max.
  • The file is appended to continuously. Keep the maximum current.

Where it's asked

  • Google · First round: coding · Last asked Dec 202545 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI

What it tests

concurrency · streaming · files

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding