Skip to the content

The emptiest shelf

Problem

The librarian looks at the empty spots on the shelf and wants the number: which day had the most books out at once, and how many were out? A book is out from the day it's borrowed until the day it comes back.

The count is taken at closing each day: a book that came back that day is already back on the shelf and doesn't count, and one that went out that day does. The ones that haven't come back are still out today, January 15, 2026.

Show a single row with that day and the number of books that were out, in that order. If the high was reached on more than one day, show the first of them.

What it has to return

These rows, in any order

daystill_out
2026-01-135

This is what comes out with the data in the practice database this challenge uses. The column names don’t count, so name yours however you like: what counts is how many there are and the order they come in. Besides this one, the challenge has hidden tests with other data that are revealed when you submit your solution.

You start with this

SQL

SELECT borrowed, returned
FROM loans
ORDER BY borrowed
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(2ⁿ) challenges

See all challenges →