File-Backed Dense Game of Life
Updated
Problem statement
Compute the next generation of a Game of Life board stored as rows in a file. Update all cells at the same time. The board may be dense. The cell-update logic is supplied; your task is to manage a sliding window of input rows and append the output rows.
Reading the file
The read-only array fileRows represents an input file stored one row after another. Every row is a binary string of the same positive width. Reading fileRows[r] corresponds to read(inputName, r * width, width). Appending a row to the returned array corresponds to append(outputName, row). Return one output row per input row, in order, without modifying the input.
Use O(width) extra working memory. Do not count the read-only input or the output rows you append toward this limit. Keep only a constant number of old rows in the active window. The tests use small boards. Explain how the same row reads and appends scale to a 1000000 x 1000000 dense board, where the entire file cannot be loaded into memory.
Outside the board, all cells are dead. Every cell in the next generation must use the original generation. There is no wrapping, and the board may be dense; a sparse live-cell representation is not an assumption.
Supplied cell-rule helper
The helper below is given to you. You may copy it unchanged; the exercise is arranging old input rows into a sliding window and appending output rows. This row wrapper applies the already supplied cell rule to one row.
Python
def processLifeRow(top, middle, bottom):
out = []
for c in range(len(middle)):
neighbors = 0
for line in (top, middle, bottom):
for d in (-1, 0, 1):
j = c + d
if 0 <= j < len(middle):
neighbors += line[j] == '1'
neighbors -= middle[c] == '1'
out.append('1' if neighbors == 3 or (middle[c] == '1' and neighbors == 2) else '0')
return ''.join(out)
Java
private String processLifeRow(String top, String middle, String bottom) {
StringBuilder out=new StringBuilder();
for(int c=0;c<middle.length();c++) {
int neighbors=0;
for(String line:new String[]{top,middle,bottom}) for(int d=-1;d<=1;d++) {
int j=c+d; if(j>=0 && j<middle.length() && line.charAt(j)=='1') neighbors++;
}
if(middle.charAt(c)=='1') neighbors--;
out.append(neighbors==3 || (middle.charAt(c)=='1' && neighbors==2)?'1':'0');
}
return out.toString();
}
C++
string processLifeRow(const string& top, const string& middle, const string& bottom) {
string out(middle.size(),'0');
for(int c=0;c<(int)middle.size();c++) {
int neighbors=0;
for(const string* line:{&top,&middle,&bottom}) for(int d=-1;d<=1;d++) {
int j=c+d; if(j>=0 && j<(int)middle.size() && (*line)[j]=='1') neighbors++;
}
neighbors-=middle[c]=='1';
if(neighbors==3 || (middle[c]=='1' && neighbors==2)) out[c]='1';
}
return out;
}
Go
func processLifeRow(top string, middle string, bottom string) string {
out:=make([]byte,len(middle))
for c:=range out {
neighbors:=0
for _,line:=range []string{top,middle,bottom} { for d:=-1;d<=1;d++ {
j:=c+d; if j>=0 && j<len(middle) && line[j]=='1' { neighbors++ }
} }
if middle[c]=='1' { neighbors-- }
out[c]='0'; if neighbors==3 || (middle[c]=='1' && neighbors==2) { out[c]='1' }
}
return string(out)
}
Function
nextFileGeneration(fileRows: String[]) → String[]Examples
Example 1
fileRows = ["000","111","000"]return = ["010","010","010"]A horizontal blinker becomes vertical synchronously.
Example 2
fileRows = ["11","11"]return = ["11","11"]A two-by-two block stays stable at dead boundaries.
Example 3
fileRows = ["1"]return = ["0"]One live cell has no neighbors and dies.
Constraints
1 <= fileRows.length, width <= 64in the tests.- Each row has exactly
widthcharacters, each either0or1. - The input is immutable and the output is a separate sequence of appended rows.
- The supplied helper uses all eight neighbors, with dead padding beyond each edge.
More Applied Intuition interview questions
- KV Store with Nested TransactionsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Parse Nested Message TypesPHONE SCREEN · Seen Nov 2026
- Resolve Variable Equations with Dependency ErrorsPHONE SCREEN · Seen Nov 2026
- Locate Vehicles with Square QueriesPHONE SCREEN · Seen Nov 2026
- Merge Collinear Point SegmentsPHONE SCREEN · Seen Oct 2026
- Compilation Order with Topological SortPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Merge Overlapping Closed IntervalsPHONE SCREEN · Seen Sep 2026
- Ring Buffer OperationsPHONE SCREEN · Seen Aug 2026
- Out-of-Order TLV File ReassemblyPHONE SCREEN · Seen Aug 2026
- Hash Map with Separate ChainingONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Sliding Window Attention over a KV CacheNVIDIA · PHONE SCREENNVIDIA interview questions
- Days Until Poisoned Plants StabilizeApple · PHONE SCREENApple interview questions
- Apply a 2048 Board MoveGlean · PHONE SCREENGlean interview questions
- Dense Matrix MultiplicationAmazon · PHONE SCREEN
- Tilting Maze BallHudson River Trading · PHONE SCREENHudson River Trading interview questions
- Spiral Order with ObstaclesTemu · PHONE SCREENTemu interview questions
- Sliding-Window API Rate LimiterPatreon · PHONE SCREENPatreon interview questions
- Logistic Regression DiagnosticsLinkedIn · PHONE SCREENLinkedIn interview questions
- Spiral Matrix TraversalByteDance · PHONE SCREENByteDance interview questions