The Grep with Context Lines problem is a classic software engineering interview question, frequently reported in technical screens for companies like xAI and Verily. It tests your ability to handle data streams, manage memory efficiently, and handle overlapping output ranges. 0 1 2
Implement a simplified version of the Unix grep command that supports the context flag (-C or --context).
When implemented as an online algorithm (processing lines one by one), the challenge is that you don't know if a line needs to be printed until you see a match later, or you might need to keep printing lines after a match has already passed. Glassdoor +1 1 2
Use a Circular Buffer (or a Deque) of size k to store the most recent lines. When a match is found, print all lines currently in the buffer.
Maintain a counter lines_to_print. When a match is found, set this counter to k. While the counter is >0is greater than 0>0, continue printing every incoming line and decrement the counter.
If a new match is found while the lines_to_print counter is still active, simply reset the counter to k. This naturally merges the context windows without duplicate printing.
Interviewers at xAI often extend this problem to test system-level thinking: 0
The core of the problem is implementing a sliding window (usually via a circular buffer) to track preceding lines and a counter to track succeeding lines, ensuring that overlapping blocks are merged into a single output stream.
How would you like to handle extremely large files where the context kk𝑘 might exceed available RAM?
[0] - Grep With Context Lines | 1Point3Acres [1] - Implement grep with context as an online algorithm. - Glassdoor [2] - Implement grep with context as an online algorithm. - Glassdoor