Skip to the content

The store on a streak

Problem

The owner swears orders come in streaks: a few days in a row something comes in, and then the store goes quiet. The owner wants to see them. A streak is a run of calendar days in a row with at least one order; a day with none breaks it, and a day on its own is a streak of 1.

For each streak, show its first day, its last day, and how many days it lasts, in that order. The one from January 9 to 12 lasts 4, and it makes no difference that two orders came in on the 12th: it's a single day.

The longest streak goes on top; between two of the same length, the earlier one goes first. Dates count the way they're written down: the order dated the 20th still hasn't been fixed, and that day is a streak all by itself.

What it has to return

These rows, in this order

first_daylast_daydays
2026-01-092026-01-124
2026-01-032026-01-042
2026-01-142026-01-152
2026-01-202026-01-201

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 DISTINCT placed
FROM orders
ORDER BY placed
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 →