Implement a simplified grep with case-insensitive, invert and exact-match flags

Quick Overview

Implement a simplified Unix grep that filters the lines of a file by a literal pattern, with flags for case-insensitive, inverted and exact whole-line matching. It tests careful string handling, combining independent options in the right order, and keeping the input order in the output.

Implement a simplified grep with case-insensitive, invert and exact-match flags

Company: Bloomberg

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Implement a simplified version of the Unix `grep` command. Given a list of strings `lines` representing the lines of a file, a `pattern` string, and a `flags` string, return all lines that match the pattern according to the rules defined by the flags. By default, a line matches if it contains `pattern` as a substring (case-sensitive). The `flags` string may contain any combination of the following characters, in any order: - `i`: case-insensitive matching. Both the line and the pattern are compared without regard to letter case. - `v`: invert match. Return the lines that do not match the pattern (after applying the other flags). - `x`: exact line match. The entire line must equal the pattern (subject to the `i` flag); containing it is not enough. Return the matching lines in the same order they appear in `lines`. ### Function Signature ```python def grep(lines: list[str], pattern: str, flags: str) -> list[str]: ``` ### Rules - The pattern is a literal string. Characters such as `.` or `*` have no special meaning. - The order of characters in `flags` does not matter: `"vi"` and `"iv"` mean the same thing. - Each returned line appears exactly as it is in the input, with its original case. Duplicate lines are returned as separate entries. - With `v`, a line is returned exactly when it would not be returned without `v`. ### Constraints - `1 <= len(lines) <= 10^4` - `0 <= len(lines[i]) <= 10^3` - `1 <= len(pattern) <= 10^3` - `flags` contains only characters from `{'i', 'v', 'x'}` with no duplicates, so `0 <= len(flags) <= 3`. - All strings consist of printable ASCII characters. ### Examples ```text Input: lines = ["Hello World", "goodbye world", "HELLO there", "nothing here"], pattern = "hello", flags = "i" Output: ["Hello World", "HELLO there"] ``` With `i`, matching ignores case. "Hello World" and "HELLO there" both contain "hello" once case is folded, and the other two lines do not contain it at all. ```text Input: lines = ["error: file not found", "warning: low memory", "info: started", "error: timeout"], pattern = "error", flags = "v" Output: ["warning: low memory", "info: started"] ``` The `v` flag inverts the match, so the result is the lines that do not contain "error", in input order. ```text Input: lines = ["abc", "ABC", "abcd", ""], pattern = "Abc", flags = "vxi" Output: ["abcd", ""] ``` With `x` and `i`, a line matches only when it equals "abc" ignoring case, which holds for "abc" and "ABC". The `v` flag then keeps the other two lines, including the empty one.

Overview: Implement a simplified Unix grep that filters the lines of a file by a literal pattern, with flags for case-insensitive, inverted and exact whole-line matching. It tests careful string handling, combining independent options in the right order, and keeping the input order in the output.

Bloomberg logo
Bloomberg
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Implement a simplified version of the Unix grep command. Given a list of strings lines representing the lines of a file, a pattern string, and a flags string, return all lines that match the pattern according to the rules defined by the flags.

By default, a line matches if it contains pattern as a substring (case-sensitive). The flags string may contain any combination of the following characters, in any order:

  • i : case-insensitive matching. Both the line and the pattern are compared without regard to letter case.
  • v : invert match. Return the lines that do not match the pattern (after applying the other flags).
  • x : exact line match. The entire line must equal the pattern (subject to the i flag); containing it is not enough.

Return the matching lines in the same order they appear in lines.

Function Signature

def grep(lines: list[str], pattern: str, flags: str) -> list[str]:

Rules

  • The pattern is a literal string. Characters such as . or * have no special meaning.
  • The order of characters in flags does not matter: "vi" and "iv" mean the same thing.
  • Each returned line appears exactly as it is in the input, with its original case. Duplicate lines are returned as separate entries.
  • With v , a line is returned exactly when it would not be returned without v .

Constraints

  • 1 <= len(lines) <= 10^4
  • 0 <= len(lines[i]) <= 10^3
  • 1 <= len(pattern) <= 10^3
  • flags contains only characters from {'i', 'v', 'x'} with no duplicates, so 0 <= len(flags) <= 3 .
  • All strings consist of printable ASCII characters.

Examples

Input: lines = ["Hello World", "goodbye world", "HELLO there", "nothing here"], pattern = "hello", flags = "i"
Output: ["Hello World", "HELLO there"]

With i, matching ignores case. "Hello World" and "HELLO there" both contain "hello" once case is folded, and the other two lines do not contain it at all.

Input: lines = ["error: file not found", "warning: low memory", "info: started", "error: timeout"], pattern = "error", flags = "v"
Output: ["warning: low memory", "info: started"]

The v flag inverts the match, so the result is the lines that do not contain "error", in input order.

Input: lines = ["abc", "ABC", "abcd", ""], pattern = "Abc", flags = "vxi"
Output: ["abcd", ""]

With x and i, a line matches only when it equals "abc" ignoring case, which holds for "abc" and "ABC". The v flag then keeps the other two lines, including the empty one.