Validate alternating checkout/return logs
Company: Meta
Role: Data Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates the ability to process sequential event logs, maintain per-entity state invariants (checkout vs return), and reason about algorithmic time and space complexity within a validation task.
Constraints
- 0 <= number of logs <= 10^5
- Each log is a tuple (timestamp, book_id, is_checkout).
- timestamps are unique integers; logs may be given in any order.
- book_id is a string; is_checkout is a boolean.
- An empty log list is valid (returns True).
Examples
Input: ([(1, 'b1', True), (2, 'b1', False), (3, 'b1', True), (4, 'b1', False)],)
Expected Output: True
Explanation: Single book b1: checkout, return, checkout, return — a clean alternation starting with a checkout.
Input: ([(1, 'b1', False)],)
Expected Output: False
Explanation: The only event for b1 is a return with no preceding checkout, so the sequence does not start with a checkout.
Hints
- Sort the logs by timestamp first, since they can arrive out of order.
- Track, per book_id, whether the next expected event is a checkout or a return. The first expected event for any book is a checkout.
- If any event does not match the expected type for its book, the whole input is invalid. A return before any checkout is just the special case where the very first expected event (checkout) is violated.