FastPrepExtract Error Log Context
Problem · Array

Extract Error Log Context

Learn this problem
EasyFlexTrade logoFlexTradeNEW GRADONSITE INTERVIEW

Problem statement

You are given a log file as an array of strings lines in original line order.

Find the earliest line that contains the case-sensitive substring ERROR. Return a new array containing up to two available lines before it, the error line itself, and up to two available lines after it, preserving their original order.

If no line contains ERROR, return an empty array.

Function

extractErrorContext(lines: String[]) → String[]

Examples

Example 1

lines = ["INFO start","DEBUG cache","ERROR timeout","INFO retry","INFO done","DEBUG close"]return = ["INFO start","DEBUG cache","ERROR timeout","INFO retry","INFO done"]

The earliest error is at index 2, so the result spans indices 0 through 4.

Example 2

lines = ["ERROR failed","INFO recovered","DEBUG done"]return = ["ERROR failed","INFO recovered","DEBUG done"]

The error is the first line, so no preceding lines are available and both following lines are returned.

Example 3

lines = ["INFO start","WARN retry"]return = []

No line contains the case-sensitive marker ERROR.

Constraints

  • 0 <= lines.length <= 200000
  • 0 <= lines[i].length <= 1000
  • Every line contains only printable ASCII characters.

More FlexTrade problems

drafts saved locally
public String[] extractErrorContext(String[] lines) {
  // Write your code here.
}
lines["INFO start","DEBUG cache","ERROR timeout","INFO retry","INFO done","DEBUG close"]
expected["INFO start", "DEBUG cache", "ERROR timeout", "INFO retry", "INFO done"]
checking account