Extract Error Log Context
Reported by candidates from FlexTrade's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
FlexTrade's September 2026 OA has a log problem that looks too easy, and that's the trap. You get an array of log lines, find the first one containing the case-sensitive substring ERROR, and return up to two lines before it, the line itself, and up to two after. No ERROR means an empty array. It's a single linear scan plus a clamped slice. Candidates lose points on boundaries and case, not on algorithms. If you blank mid-assessment, StealthCoder runs invisibly as a safety net, but this one is quick enough to nail by hand.
The problem
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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The pattern is a simple array scan. Loop through lines in order and stop at the first index i where lines[i] contains "ERROR". Use a case-sensitive substring check, so "error" or "Error" must not match. Then compute start = max(0, i - 2) and end = min(n - 1, i + 2), and return the slice from start to end inclusive. If the loop finishes with no match, return an empty array. The common pitfalls are off-by-one on the slice end, since many languages use an exclusive upper bound, and returning the last error instead of the earliest. Also handle an empty input array, which is allowed by the constraints. With up to 200000 lines of up to 1000 characters, one pass is fine. Don't build extra structures. If you freeze on the slice math, StealthCoder is the hedge for the live OA, reading the problem and giving you the clamped-index version.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Extract Error Log Context cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass FlexTrade's OA.
FlexTrade reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Extract Error Log Context FAQ
How hard is the FlexTrade Extract Error Log Context question really?+
It's easy. One linear scan and a clamped slice. The difficulty is in edge cases: error at index 0 or the last index, an empty array, and no match. If you test those three examples by hand, you're basically done.
What's the trick to this problem?+
Clamp your window. Start at max(0, i - 2) and end at min(n - 1, i + 2). Stop at the first match so you return the earliest error. There's no need for regex, sorting, or extra data structures.
Does the ERROR match need to be case-sensitive?+
Yes. The statement says case-sensitive substring, so only the exact uppercase ERROR counts. Don't lowercase the lines before checking. A line like "error timeout" or "Error" should not trigger a match. Use a plain contains or indexOf check.
What should I return when no line contains ERROR?+
Return an empty array. Example 3 shows this with lines like INFO start and WARN retry. Also remember that an empty input array falls into the same case, since the scan finds nothing and you return empty.
How do I prepare for this in 48 hours?+
Practice array scanning with index clamping and inclusive slice bounds in your chosen language. Write this solution once from memory, then run all three examples plus an error at the last index. That covers nearly every way this question can go wrong.