Problem · Array

Read a File from Distributed Chunks

Learn this problem
MediumHudson River Trading logoHudson River TradingFULLTIMEONSITE INTERVIEW

Problem statement

A logical file consists of chunkCount chunks with required identifiers from 0 through chunkCount - 1. The arrays chunkIds, chunkData, and readable describe unordered responses from distributed storage locations.

Return the logical file by concatenating the chunk data in increasing identifier order.

Return READ_ERROR instead when any of these conditions holds:

  • a required identifier is missing;
  • an identifier is duplicated or outside the required range;
  • the response arrays have different lengths; or
  • any required chunk is not readable.

A readable chunk may contain an empty string. Its data is still part of the logical file.

Function

readLogicalFile(chunkCount: int, chunkIds: int[], chunkData: String[], readable: boolean[]) → String

Examples

Example 1

chunkCount = 3chunkIds = [2,0,1]chunkData = ["!","Hello ","world"]readable = [true,true,true]return = "Hello world!"

The storage responses are unordered, but identifiers 0, 1, and 2 reconstruct the file in that order.

Example 2

chunkCount = 3chunkIds = [0,1,1]chunkData = ["a","b","c"]readable = [true,true,true]return = "READ_ERROR"

Identifier 1 is duplicated and identifier 2 is missing.

Example 3

chunkCount = 2chunkIds = [1,0]chunkData = ["tail","head"]readable = [false,true]return = "READ_ERROR"

The chunk with identifier 1 cannot be read, so the logical file is not returned.

Constraints

  • 1 <= chunkCount <= 10^5
  • 0 <= chunkIds.length, chunkData.length, readable.length <= 10^5
  • The total length of all strings in chunkData is at most 2 * 10^5.
  • Chunk data contains printable ASCII characters.

More Hudson River Trading problems

drafts saved locally
public String readLogicalFile(int chunkCount, int[] chunkIds, String[] chunkData, boolean[] readable) {
  // write your code here
}
chunkCount3
chunkIds[2,0,1]
chunkData["!","Hello ","world"]
readable[true,true,true]
expected"Hello world!"
checking account