FastPrepExtensible Grep Line Filter

Extensible Grep Line Filter

Bloomberg LP logoBloomberg LP● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

A text file is serialized as the array lines. Return every original line selected by a simplified grep command, preserving file order and duplicate lines.

A line initially matches when it contains pattern as a contiguous substring. The unique flags in flags may appear in either order:

  • -i makes substring matching case-insensitive for ASCII letters.
  • -v inverts the final match decision.

An empty pattern matches every line before optional inversion. Return original line text without normalization.

Function

grepLines(lines: String[], pattern: String, flags: String[]) → String[]

Examples

Example 1

lines = ["error: disk full","INFO ready","network error"]pattern = "error"flags = []return = ["error: disk full","network error"]

The first and third lines contain the case-sensitive substring error.

Example 2

lines = ["Error","all good","ERROR again"]pattern = "error"flags = ["-i"]return = ["Error","ERROR again"]

The -i flag ignores ASCII letter case while returned lines keep their original text.

Example 3

lines = ["warn","ok","WARN"]pattern = "warn"flags = ["-v","-i"]return = ["ok"]

Case-insensitive matching selects the two warning lines, then -v returns their complement.

Constraints

  • 0 <= lines.length <= 10000.
  • 0 <= lines[i].length <= 500.
  • 0 <= pattern.length <= 100.
  • 0 <= flags.length <= 2.
  • Every line and pattern contains printable ASCII characters.
  • flags contains unique values chosen from -i and -v.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String[] grepLines(String[] lines, String pattern, String[] flags) {
    // Write your code here.
}
lines["error: disk full","INFO ready","network error"]
pattern"error"
flags[]
expected["error: disk full", "network error"]
Checking account…