Grep With Context Lines

Quick Overview

Return every line of a file that contains a substring, plus up to k lines of context before and after each match, in original order and without repeats. It tests merging overlapping index windows, deduplicating by position, and keeping the work linear when many matches sit close together.

Grep With Context Lines

Company: Snowflake

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array of strings `lines` holding the contents of a file, a string `pattern`, and an integer `k`, return all lines that either contain `pattern` as a substring or fall within `k` lines before or after a matching line. The returned lines keep their original order, and each line appears only once in the result, even if it falls within the context window of several matches. ### Function Signature ```python def grep_with_context(lines: list[str], pattern: str, k: int) -> list[str]: ``` ### Rules - A line matches when `pattern` occurs in it as a contiguous, case-sensitive substring. `pattern` is literal text, so characters such as `.` or `*` have no special meaning. - With `n = len(lines)`, a match at index `i` selects every index from `max(0, i - k)` to `min(n - 1, i + k)`, inclusive. - "Only once" refers to positions in `lines`. Two equal strings at different indices are different lines, and both appear in the result if both are selected. - If no line matches, return an empty list. ### Constraints - `1 <= len(lines) <= 10^4` - `0 <= len(lines[i]) <= 200` - `1 <= len(pattern) <= 100` - `0 <= k <= len(lines)` ### Examples ```text Input: lines = ["alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta"], pattern = "delta", k = 1 Output: ["gamma", "delta", "epsilon"] ``` Only index 3 contains `"delta"`. With `k = 1` it selects indices 2 through 4. ```text Input: lines = ["foo", "bar", "baz", "foo", "qux"], pattern = "foo", k = 1 Output: ["foo", "bar", "baz", "foo", "qux"] ``` `"foo"` matches at indices 0 and 3. The windows are indices 0 to 1 and 2 to 4, which together cover all five lines. `"foo"` appears twice because it sits at two different indices. ```text Input: lines = ["error: disk", "ok", "ok", "ok", "warn", "error: net"], pattern = "error", k = 1 Output: ["error: disk", "ok", "warn", "error: net"] ``` Matches at indices 0 and 5 select indices 0 to 1 and 4 to 5. Indices 2 and 3 are outside both windows, so they are left out.

Overview: Return every line of a file that contains a substring, plus up to k lines of context before and after each match, in original order and without repeats. It tests merging overlapping index windows, deduplicating by position, and keeping the work linear when many matches sit close together.

Snowflake logo
Snowflake
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an array of strings lines holding the contents of a file, a string pattern, and an integer k, return all lines that either contain pattern as a substring or fall within k lines before or after a matching line.

The returned lines keep their original order, and each line appears only once in the result, even if it falls within the context window of several matches.

Function Signature

def grep_with_context(lines: list[str], pattern: str, k: int) -> list[str]:

Rules

  • A line matches when pattern occurs in it as a contiguous, case-sensitive substring. pattern is literal text, so characters such as . or * have no special meaning.
  • With n = len(lines) , a match at index i selects every index from max(0, i - k) to min(n - 1, i + k) , inclusive.
  • "Only once" refers to positions in lines . Two equal strings at different indices are different lines, and both appear in the result if both are selected.
  • If no line matches, return an empty list.

Constraints

  • 1 <= len(lines) <= 10^4
  • 0 <= len(lines[i]) <= 200
  • 1 <= len(pattern) <= 100
  • 0 <= k <= len(lines)

Examples

Input: lines = ["alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta"], pattern = "delta", k = 1
Output: ["gamma", "delta", "epsilon"]

Only index 3 contains "delta". With k = 1 it selects indices 2 through 4.

Input: lines = ["foo", "bar", "baz", "foo", "qux"], pattern = "foo", k = 1
Output: ["foo", "bar", "baz", "foo", "qux"]

"foo" matches at indices 0 and 3. The windows are indices 0 to 1 and 2 to 4, which together cover all five lines. "foo" appears twice because it sits at two different indices.

Input: lines = ["error: disk", "ok", "ok", "ok", "warn", "error: net"], pattern = "error", k = 1
Output: ["error: disk", "ok", "warn", "error: net"]

Matches at indices 0 and 5 select indices 0 to 1 and 4 to 5. Indices 2 and 3 are outside both windows, so they are left out.