FastPrepRead N Bytes with read4096 Across Calls
Problems/Read N Bytes with read4096 Across Calls

Read N Bytes with read4096 Across Calls

Hudson River Trading logoHudson River Trading● MediumINTERNPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report comes from the second phone round of a Hudson River Trading core developer internship. The candidate had applied for a Python role but was interviewed in C++. One task was to build reading on top of a primitive that reads a file 4096 bytes at a time.

The report gives no signature, read sizes, end-of-file rules or examples. Here a reader answers a list of read(n) calls on one file and keeps unused bytes between calls, as in the common read4 exercise. The limits and examples are our choices, and any language is allowed.

Our version is about 65% the same as the reported question.

A file can only be read through one low-level call, read4096(buf). Each call copies up to the next 4096 bytes of the file into buf and returns how many bytes it copied. It copies fewer than 4096 bytes only at the end of the file, and it returns 0 once the whole file has been read.

Build a reader on top of it with one method, read(n). Each call returns the next n bytes that no earlier call returned, or all of the remaining bytes if fewer than n are left. A call to read4096 often brings in more bytes than read(n) needs. Keep those extra bytes and hand them out first in later calls. read(0) returns an empty string and uses up nothing.

In this exercise, file holds the contents of the file, and each character is one byte. reads lists the n of each call to read(n), in order, all on the same reader. Return what each call returns, in the same order.

Write read4096 as a small helper over file, and let your reader get bytes only through it. A simple solution may copy the whole file first. Try to keep at most one 4096-byte block in memory instead.

Function

readBytes(file: String, reads: int[]) → String[]

Examples

Example 1

file = "hello world"reads = [5,1,100]return = ["hello"," ","world"]

The file has 11 bytes. The first call, read(5), finds the buffer empty and calls read4096 once. Because the file is shorter than 4096 bytes, that call returns all 11 bytes. The reader returns "hello" and keeps the other 6 bytes in its buffer.

read(1) takes " " from the buffer without calling read4096. read(100) takes the last 5 bytes, "world". Then read4096 returns 0, so this call stops with fewer than 100 bytes.

Example 2

file = "abc"reads = [0,2,0,5,3]return = ["","ab","","c",""]

read(0) returns an empty string and uses up nothing. read(2) returns "ab". The next read(0) again returns an empty string, and the byte c stays in the buffer.

read(5) gets only "c", because the file ends. The last call, read(3), is past the end of the file and returns an empty string.

Example 3

file = "123456789"reads = [3,3,3,3]return = ["123","456","789",""]

Each call returns the next 3 bytes: "123", "456", then "789". The fourth call finds nothing left and returns an empty string.

Constraints

  • 0 <= file.length <= 10^4
  • file contains only printable ASCII characters, with codes from 32 to 126.
  • 1 <= reads.length <= 1000
  • 0 <= reads[i] <= 10^4

More Hudson River Trading Phone Screen questions

  • Format a Signed Integer with CommasPHONE SCREEN · Seen Nov 2026
  • Order and Batch Open-Close LifecyclePHONE SCREEN · Seen Nov 2026
  • Tilting Maze BallPHONE SCREEN · Seen Oct 2026
  • Tiered Expiring Item StorePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Market Data Signal WatcherPHONE SCREEN · Seen Jul 2026
  • Integer to String Without Built-insPHONE SCREEN · Seen May 2026
  • Contact Tracing Through Timed MeetingsPHONE SCREEN · Seen Nov 2024
  • Polydivisible Pandigital NumbersPHONE SCREEN · Seen Feb 2024
  • Defragment a Buffer of ObjectsPHONE SCREEN · Seen Nov 2023
  • Call Auction Clearing Price and Order FillsPHONE SCREEN · Seen Nov 2022

Similar questions at other companies

  • Elevator Scheduling SimulationOpenAI · PHONE SCREENOpenAI interview questions
  • Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
  • Load/Store Arithmetic InterpreterJane Street · PHONE SCREENJane Street interview questions
  • Parking Garage AllocatorAmazon · PHONE SCREEN
  • Variadic Function Library MatchesIBM · PHONE SCREENIBM interview questions
  • Project and Filter Structured RecordsStripe · PHONE SCREENStripe interview questions
  • Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
  • Ordered IP FirewallDatabricks · PHONE SCREENDatabricks interview questions
  • Add Signed Character ArraysByteDance · PHONE SCREENByteDance interview questions
  • Is This Tic-Tac-Toe Board ReachableAdobe · PHONE SCREENAdobe interview questions

Hudson River Trading interview experiences

  • Hudson River Trading Quant Video Interview: 1 Experience
  • HRT SWE Intern and Onsites: 34 Experiences (Part 2 of 2)
  • HRT Python Track Interviews: 64 Experiences (Part 1 of 2)
  • HRT New Grad Python Track Interviews: 9 Experiences
All Hudson River Trading interview questionsSee Hudson River Trading hiring insights
Saved
class Solution {
    public String[] readBytes(String file, int[] reads) {
        // Write your code here.
    }
}
Checking account…