PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates the ability to parse JSON logs, apply time-window filtering with inclusive/exclusive boundaries, aggregate error frequencies, and implement sorting with lexicographic tie-breaking.

  • medium
  • Notion
  • Coding & Algorithms
  • Software Engineer

Find Top Errors in Time Window

Company: Notion

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Implement a function that analyzes request-response logs stored as JSON records. Each record contains a timestamp, a response status, and optionally an error code or error message. Given startTime, endTime, and k, return the k most frequent errors among records whose timestamps fall in the target interval. For this problem, use a half-open interval: startTime is inclusive and endTime is exclusive. Ignore successful responses and records with no error value. Input details: - recordsJson: a JSON array of request records. - Each record has timestamp as an ISO-8601 UTC string, status as an integer HTTP-like status code, and error as either a string or null. - startTime and endTime are ISO-8601 UTC strings. - k is a positive integer. Output details: - Return a list of pairs containing error and count. - Sort by descending count. - Break ties by lexicographically ascending error string. - If fewer than k distinct errors exist, return all of them. Example: records contain these entries: 10:00 TIMEOUT with status 500, 10:01 AUTH_FAILED with status 401, 10:02 TIMEOUT with status 503, 10:10 DB_ERROR with status 500. With startTime 10:00, endTime 10:10, and k = 2, the output is TIMEOUT with count 2, then AUTH_FAILED with count 1. The 10:10 record is excluded because the end time is exclusive. Clarify in the interview whether the interval boundaries are inclusive or exclusive before coding.

Quick Answer: This question evaluates the ability to parse JSON logs, apply time-window filtering with inclusive/exclusive boundaries, aggregate error frequencies, and implement sorting with lexicographic tie-breaking.

Implement a function `solution(recordsJson, startTime, endTime, k)` that analyzes request logs stored as a JSON array. Each log record contains a `timestamp`, a numeric `status`, and may contain an `error` field whose value is either a string or `null`. Count errors only for records that satisfy all of the following: - `startTime <= timestamp < endTime` (half-open interval) - `status >= 400` - `error` exists and is not `null` Return the `k` most frequent distinct errors as a list of `[error, count]` pairs. Sorting rules: 1. Higher count comes first. 2. If two errors have the same count, sort by lexicographically ascending error string. 3. If fewer than `k` distinct errors exist, return all of them. For this problem, all timestamps are normalized ISO-8601 UTC strings, so they can be compared directly as strings.

Constraints

  • 0 <= number of records <= 100000
  • 1 <= k <= 100000
  • All timestamps are valid UTC strings in normalized format YYYY-MM-DDTHH:MM:SSZ
  • A record with status < 400 must be ignored, even if it has an error value
  • The `error` field may be missing or null; such records must be ignored

Examples

Input: ('[{"timestamp":"2024-06-01T10:00:00Z","status":500,"error":"TIMEOUT"},{"timestamp":"2024-06-01T10:01:00Z","status":401,"error":"AUTH_FAILED"},{"timestamp":"2024-06-01T10:02:00Z","status":503,"error":"TIMEOUT"},{"timestamp":"2024-06-01T10:05:00Z","status":200,"error":"IGNORED"},{"timestamp":"2024-06-01T10:10:00Z","status":500,"error":"DB_ERROR"}]', '2024-06-01T10:00:00Z', '2024-06-01T10:10:00Z', 2)

Expected Output: [['TIMEOUT', 2], ['AUTH_FAILED', 1]]

Explanation: The record at 10:10 is excluded because endTime is exclusive. The 200-status record is ignored. TIMEOUT appears twice and AUTH_FAILED once.

Input: ('[{"timestamp":"2024-06-01T09:00:00Z","status":500,"error":"CACHE_MISS"},{"timestamp":"2024-06-01T09:01:00Z","status":500,"error":"DB_ERROR"},{"timestamp":"2024-06-01T09:02:00Z","status":503,"error":"DB_ERROR"},{"timestamp":"2024-06-01T09:03:00Z","status":404,"error":"CACHE_MISS"},{"timestamp":"2024-06-01T09:04:00Z","status":500,"error":null},{"timestamp":"2024-06-01T09:05:00Z","status":200,"error":"SHOULD_NOT_COUNT"}]', '2024-06-01T09:00:00Z', '2024-06-01T09:06:00Z', 5)

Expected Output: [['CACHE_MISS', 2], ['DB_ERROR', 2]]

Explanation: Both errors appear twice. The tie is broken lexicographically, so CACHE_MISS comes before DB_ERROR. Null errors and successful responses are ignored.

Input: ('[]', '2024-06-01T00:00:00Z', '2024-06-02T00:00:00Z', 3)

Expected Output: []

Explanation: There are no records, so there are no errors to return.

Input: ('[{"timestamp":"2024-06-01T00:00:00Z","status":500,"error":"A"},{"timestamp":"2024-06-01T00:30:00Z","status":500,"error":"B"},{"timestamp":"2024-06-01T01:00:00Z","status":500,"error":"C"}]', '2024-06-01T00:30:00Z', '2024-06-01T01:00:00Z', 10)

Expected Output: [['B', 1]]

Explanation: A is before the window, B is inside the window, and C is excluded because endTime is exclusive.

Input: ('[{"timestamp":"2024-06-01T12:00:00Z","status":500},{"timestamp":"2024-06-01T12:01:00Z","status":500,"error":"X"},{"timestamp":"2024-06-01T12:01:30Z","status":399,"error":"Y"}]', '2024-06-01T12:00:00Z', '2024-06-01T12:02:00Z', 2)

Expected Output: [['X', 1]]

Explanation: The first record has no error field, so it is ignored. The third record has status 399, so it is not counted as an error.

Hints

  1. Use a hash map / dictionary to count how many times each error appears after filtering records.
  2. Because every timestamp is in the same normalized UTC format, you can check the time window using simple string comparison.
Last updated: Jun 6, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement a DAG task graph - Notion (medium)
  • Implement Table Aggregation - Notion (easy)
  • Implement text editor with undo/redo - Notion (Medium)
  • Design a text editor with undo/redo - Notion (Medium)