← Questions

Validate checkout and return logs

0
MetaCodingLast asked Sep 2025

A coding interview question asked in Meta's technical phone screen, last asked Sep 2025. It tests python, hash maps and data engineering.

A library logs every checkout and return as (timestamp, book_id, is_checkout), where is_checkout is True for a checkout and False for a return. Check that the log is consistent: for every book, the events, in time order, start with a checkout and then strictly alternate between checkout and return.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • validate_logs(logs: list[tuple[int, str, bool]]) -> bool — True if every book's history is valid. The logs may arrive in any order, so order them by timestamp first. An empty log is valid.

Example

validate_logs([(1, "b1", True), (2, "b1", False), (3, "b1", True), (4, "b1", False)])  ->  True
validate_logs([(1, "b1", False)])                                                      ->  False  # returned before any checkout
validate_logs([(1, "b1", True), (2, "b1", True)])                                      ->  False  # checked out twice in a row
validate_logs([(3, "b2", False), (1, "b1", True), (2, "b2", True)])                    ->  True   # out of order in the input

A book that is still checked out at the end of the log is fine.

Constraints

  • 0 <= len(logs) <= 10^5. Timestamps are unique integers.

Clarifying questions

  • Are the logs already sorted by time?
  • Is a book that is still out at the end of the log valid?
  • Can two events share a timestamp?
  • Should I return only True or False, or which books are invalid?

What a strong answer covers

  • Sort by timestamp (O(n log n)), or skip the sort if the input is already in time order.
  • A dictionary from book to the next expected event: checkout at first, then flipping after every valid event. The first mismatch fails the whole log.
  • O(n) after sorting, and O(b) memory for b books.
  • Tests: empty input, a single return, interleaved books, out-of-order input.

Follow-ups

  • Return the list of books whose history is invalid, and the first bad event of each.
  • The log is a stream that never ends. Validate as events arrive.
  • Several copies of the same title: a return is valid while any copy is out.

Where it's asked

  • Meta · Technical phone screen · Last asked Sep 202545 min · Two coding problems in CoderPad, about 20 minutes each · Usually no AI; some newer screens are AI-assisted

What it tests

python · hash maps · data engineering

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding