Extensible Grep Line Filter
Updated
Problem statement
Given the lines of a text file as the array lines, a string pattern, and an array flags, return every line that a simplified grep command selects. Keep the lines in file order, keep duplicate lines, and return each line exactly as written.
A line matches when it contains pattern as a substring (characters next to each other, in order). An empty pattern matches every line. The flags in flags may come in either order:
-imakes matching ignore upper and lower case for ASCII letters.-vflips the final result, so the lines that do not match are returned.
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"]With no flags, matching is case-sensitive. Only the first and third lines contain error.
Example 2
lines = ["Error","all good","ERROR again"]pattern = "error"flags = ["-i"]return = ["Error","ERROR again"]The -i flag ignores case, so Error and ERROR again match. They are returned with their original case.
Example 3
lines = ["warn","ok","WARN"]pattern = "warn"flags = ["-v","-i"]return = ["ok"]With -i, both warn and WARN match. Then -v flips the result, so only ok is returned.
Constraints
0 <= lines.length <= 10000.0 <= lines[i].length <= 500.0 <= pattern.length <= 100.0 <= flags.length <= 2.- Every line and
patterncontains printable ASCII characters. flagscontains unique values chosen from-iand-v.
More Bloomberg LP Phone Screen questions
- Collatz Conjecture with Shared MemoizationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
- Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
- Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Sparse Vector and Matrix OperationsPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Restricted Decimal Number ValidatorLinkedIn · PHONE SCREEN
- Validate Timestamped Message RecordsStripe · PHONE SCREEN
- Format CSV Rows as an ASCII TableAmazon · PHONE SCREEN
- Compare Calendar DatesWells Fargo · PHONE SCREEN
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEW
- CSV Row SerializerMercor · PHONE SCREEN
- Host Log ParserVerkada · PHONE SCREEN
- Length-Prefixed String CodecSnowflake · PHONE SCREEN
- Machine Message RouterOpenAI · PHONE SCREEN
- Parse and Query Server LogsKikoff · PHONE SCREEN