Implement File Tail and Sensor Health
Company: Confluent
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
This interview included two coding tasks.
1. **Print the last `N` lines of a file**
You are given a very large newline-delimited text file and a limited file API:
- `read(k)`: reads the next `k` bytes from the current file pointer and advances the pointer
- `movePointer(pos)`: moves the file pointer to byte offset `pos`
- `getSize()`: returns the file size in bytes
Write pseudocode to stream the last `N` lines of the file to standard output. The file may be much larger than memory, so you should not read the entire file at once. Also discuss the tradeoffs between scanning with a reusable buffer versus moving the file pointer very frequently.
2. **Track sensor liveness from heartbeat timestamps**
A monitoring service receives heartbeats as pairs `(sensorId, timestamp)`, where `timestamp` is a discrete time slot. Implement:
- `recordPing(sensorId, timestamp)`
- `wasAlive(sensorId, now)`
A sensor is considered down if it has missed **three consecutive slots**, so `wasAlive(sensorId, now)` should return `true` if the sensor sent at least one ping during slots `[now-2, now]`, and `false` otherwise.
Assume there are many sensors and many queries. Describe or implement an efficient approach, such as using a hash map plus binary search over each sensor's ping history.
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.