Read a Character Stream across Repeated Requests

Quick Overview

Read a hidden character stream through read4, preserving unread characters across repeated requests and reporting valid buffer prefixes.

Read a Character Stream across Repeated Requests

Company: NVIDIA

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

# Read a Character Stream across Repeated Requests A hidden file is available only through `read4(buf4)`. Implement `Solution.read(buf, n)` to copy up to `n` next characters into the caller's destination buffer and return the number actually copied. The same `Solution` instance receives multiple requests for the same file, in order. ### Interface `read4(buf4)` reads up to four consecutive characters from its current file position, writes them into `buf4[0:k]`, advances the file position by `k`, and returns `k`. It returns fewer than four only when the end of the file is reached, and returns zero when no characters remain. The file does not change during these calls. For this Python practice interface, a trusted `Reader4` supplies the method. Construct `Solution(reader.read4)` and then call `read(buf, n)`. The candidate may obtain file characters only by invoking this callable; it may not inspect its owner, private attributes, closure or underlying file data. Each `read` call starts writing at `buf[0]`. Its return value `k` makes only the prefix `buf[:k]` part of the answer; the remaining cells are unspecified. The same caller buffer is reused across requests and has enough capacity for each requested `n`. Do not return a string in place of the count. Separate reader/solution instances begin independently at the start of their own files. ### Examples The file text is private test setup and is not passed to `read`. **Example 1:** File `"Q7rS2tU9"`, requests `[3, 2, 5, 1]`, using one solution and one reusable buffer: | Requested n | Returned count | Valid buffer prefix | | --- | --- | --- | | 3 | 3 | `"Q7r"` | | 2 | 2 | `"S2"` | | 5 | 3 | `"tU9"` | | 1 | 0 | `""` | **Example 2:** File `"mN4pq"`, requests `[4, 1, 2]`: | Requested n | Returned count | Valid buffer prefix | | --- | --- | --- | | 4 | 4 | `"mN4p"` | | 1 | 1 | `"q"` | | 2 | 0 | `""` | ### Constraints - The file contains `1..500` English letters or digits. - A test makes `1..10` calls to `read`. - Each request satisfies `1 <= n <= 500`. - Destination buffers are mutable lists of character cells with capacity at least the current request size. The buffer passed to `read4` has capacity at least four. - Calls for one instance are sequential. Unread characters must remain available to later requests, even if an earlier call to `read4` fetched them already. ### What a Strong Answer Covers - Fill the correct buffer prefix and return the actual count. - Preserve stream order across short reads and repeated requests. - Handle partial final reads, later calls at end-of-file, and independent instances. - Explain time and auxiliary storage across the sequence of requests. ### Follow-up Questions - What must a test compare when the caller reuses a buffer containing old characters? - What changes if the primitive reads at most `b` characters instead of four?

Overview: Read a hidden character stream through read4, preserving unread characters across repeated requests and reporting valid buffer prefixes.

NVIDIA logo
NVIDIA
hardSoftware EngineerOtherCoding & Algorithms
0
0

Read a Character Stream across Repeated Requests

A hidden file is available only through read4(buf4). Implement Solution.read(buf, n) to copy up to n next characters into the caller's destination buffer and return the number actually copied. The same Solution instance receives multiple requests for the same file, in order.

Interface

read4(buf4) reads up to four consecutive characters from its current file position, writes them into buf4[0:k], advances the file position by k, and returns k. It returns fewer than four only when the end of the file is reached, and returns zero when no characters remain. The file does not change during these calls.

For this Python practice interface, a trusted Reader4 supplies the method. Construct Solution(reader.read4) and then call read(buf, n). The candidate may obtain file characters only by invoking this callable; it may not inspect its owner, private attributes, closure or underlying file data.

Each read call starts writing at buf[0]. Its return value k makes only the prefix buf[:k] part of the answer; the remaining cells are unspecified. The same caller buffer is reused across requests and has enough capacity for each requested n. Do not return a string in place of the count. Separate reader/solution instances begin independently at the start of their own files.

Examples

The file text is private test setup and is not passed to read.

Example 1: File "Q7rS2tU9", requests [3, 2, 5, 1], using one solution and one reusable buffer:

Requested nReturned countValid buffer prefix
33"Q7r"
22"S2"
53"tU9"
10""

Example 2: File "mN4pq", requests [4, 1, 2]:

Requested nReturned countValid buffer prefix
44"mN4p"
11"q"
20""

Constraints

  • The file contains 1..500 English letters or digits.
  • A test makes 1..10 calls to read .
  • Each request satisfies 1 <= n <= 500 .
  • Destination buffers are mutable lists of character cells with capacity at least the current request size. The buffer passed to read4 has capacity at least four.
  • Calls for one instance are sequential. Unread characters must remain available to later requests, even if an earlier call to read4 fetched them already.

What a Strong Answer Covers Guidance

  • Fill the correct buffer prefix and return the actual count.
  • Preserve stream order across short reads and repeated requests.
  • Handle partial final reads, later calls at end-of-file, and independent instances.
  • Explain time and auxiliary storage across the sequence of requests.

Follow-up Questions Guidance

  • What must a test compare when the caller reuses a buffer containing old characters?
  • What changes if the primitive reads at most b characters instead of four?