Implement File Tail and Sensor Health
Company: Confluent
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates skills in large-file streaming and buffer/pointer manipulation for tailing files, along with data-structure and time-window reasoning for tracking sensor liveness from heartbeat timestamps.
Return the Last N Lines
Constraints
- n >= 0
Examples
Input: ('a\nb\nc\n', 2)
Expected Output: ['b', 'c']
Explanation: Trailing newline does not create an extra line in splitlines.
Input: ('one', 5)
Expected Output: ['one']
Explanation: n larger than line count returns all lines.
Input: ('', 3)
Expected Output: []
Explanation: Empty file.
Input: ('a\nb', 0)
Expected Output: []
Explanation: Zero requested lines.
Hints
- A real file solution scans from the end in blocks; this console uses strings for deterministic testing.
Sensor Heartbeat Liveness
Constraints
- timestamps are discrete comparable values
Examples
Input: ([(1, 5), (1, 8), (2, 3)], [(1, 7), (1, 11), (2, 5), (3, 1)])
Expected Output: [True, False, True, False]
Explanation: Queries use each sensor's sorted ping history.
Input: ([], [('a', 0)])
Expected Output: [False]
Explanation: Unknown sensor is down.
Input: ([(1, 1), (1, 1)], [(1, 1), (1, 3), (1, 4)])
Expected Output: [True, True, False]
Explanation: Duplicate timestamps are fine.
Hints
- Store timestamps per sensor and binary-search the first ping >= now-2.