Google | Final Round | L4
Anonymous User
2229

Think about the grep command. Suppose you are given an array, where each element is a line in a document we scanned. Write the grep command, which would print the lines that match a search target we are looking for. Besides the search target, we will provide another parameter linesAround which is a number, which tells you to print a number of lines before and after the match line. No line must be printed more than once.

You may ignore regex and complex options, and can use something like String.includes(searchTarget).

Example:
input ['good morning', 'hello there', 'my name is Alex', 'my friend is albert', 'it is nice to meet you Alex']
search target: Alex
linesAround = 1
we should print:
'hello there'
'my name is Alex'
'my friend is Albert'
'it is nice to meet you Alex'

notice, 'my friend is albert' was only printed once, even though it was within 1 lines away from two search targets

Follow up: instead of being given an array of lines, suppose you are being fed a stream of lines, one by one. So now, you function should only take in one line as an argument.

Comments (3)