Implement File Tail and Sensor Health
ConfluentSoftware Engineer · OnsiteLast updated
This problem tests your ability to efficiently read the end of a growing file using file pointers and to implement a sliding time-window check for sensor heartbeat events.
Part 1: File Tailing
Implement a function tail_file(filepath, n) that prints the last n lines of a file. The file may be very large (larger than memory), so you cannot load the entire file into memory. Assume the file is ASCII encoded.
Part 2: Sensor Health
You are receiving a stream of sensor heartbeats. Each heartbeat is a tuple (sensor_id, timestamp). Implement a class SensorMonitor to track the health of sensors.
record_heartbeat(sensor_id, timestamp): Stores the heartbeat.get_unhealthy_sensors(current_time, threshold): Returns a list ofsensor_ids that have not sent a heartbeat in the lastthresholdseconds, relative tocurrent_time.
Example
record_heartbeat("A", 100)
record_heartbeat("B", 105)
get_unhealthy_sensors(120, 10) -> Returns ["A"] because B's last heartbeat (105) is within the [110, 120] window, but A's (100) is not.
Constraints
- For Part 1: Minimize I/O operations and memory usage.
- For Part 2:
record_heartbeatshould beO(1)orO(log N). Thethresholdis a constant integer.
Hints
Hint 1
For file tailing, use `file.seek()` to jump to the end of the file and read backwards in chunks until you have gathered enough newline characters.
Hint 2
For sensor health, use a dictionary to store the `sensor_id` as the key and its latest `timestamp` as the value.
Hint 3
Ensure you handle edge cases for the file tailing, such as files that are smaller than the requested number of lines or empty files.
Follow-up questions
- How would you modify the file tailing solution if the file is being continuously appended to by another process?
- If the number of sensors grows to millions, how would you optimize memory usage in the SensorMonitor?
- What concurrency issues might arise if multiple threads call `record_heartbeat` and `get_unhealthy_sensors` simultaneously?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.