Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
The growth team thinks the healthiest sessions are exploratory: a visitor moves through new pages instead of bouncing between the same two. For each session they want the longest contiguous run of page views in which no page appears twice, so it can be compared against conversion.
Write longest_unique_run(pages). It takes pages, a list of page-path strings in the order they were visited, and returns a dict {"start": int | None, "length": int} giving the index where the longest repeat-free run begins and how many views it contains.
Function to write
longest_unique_run(pages: list[str]) -> dictA dict with the start index and length of the earliest longest run with no repeated page; start is None for an empty session.
How to approach it
Remember where each page was last seen. On a repeat, the left edge only moves if that last visit is still inside the window.
Sample cases
+ 3 held back until you submit
a session that loops back
Repeats force the window forward three times; three pages is the longest clean run.
Input
Argument 1
[
'/home',
'/search',
'/item',
'/home',
'/search',
'/search',
'/cart',
'/checkout'
]Returns
{
'start': 0,
'length': 3
}a stale visit before the window
The second /home repeats a visit already outside the window, so the left edge must not move back.
Input
Argument 1
[
'/home',
'/cart',
'/cart',
'/home',
'/help'
]Returns
{
'start': 2,
'length': 3
}two runs of equal length
Every two-page run is clean and the same length, so the earliest start (0) wins.
Input
Argument 1
[
'/home',
'/docs',
'/home',
'/docs'
]Returns
{
'start': 0,
'length': 2
}no visits at all
An empty session has no run, so start is None and length is 0 rather than an error.
Input
Argument 1
[] (empty list)
Returns
{
'start': None,
'length': 0
}Constraints
pages. It is valid when no page path appears in it more than once.{"start": i, "length": n} where n is the length of the longest valid run and i is its first index.{"start": None, "length": 0}./Home and /home are different pages.Worked example
Take ["/home", "/cart", "/cart", "/home", "/help"]. Keep a dict of the last index each page was seen, and a left edge starting at 0. Index 0 /home and index 1 /cart are new: the window is 0–1, length 2. Index 2 /cart was last seen at 1, which is inside the window, so the left edge jumps to 2. Index 3 /home was last seen at 0 — but 0 is before the left edge, so that visit is stale and the edge stays at 2. Index 4 /help is new: the window 2–4 has length 3. The answer is {"start": 2, "length": 3}.
The trap: code that always sets the left edge to 'previous visit + 1' moves it backwards from 2 to 1 at /home, and reports the run /cart, /cart, /home, /help of length 4 — a run that contains /cart twice.
What this tests
The variable-size sliding window with a last-seen index — LeetCode 'Longest Substring Without Repeating Characters' in clickstream clothing. The target is O(n) time and O(distinct pages) space; checking every start and scanning forward is O(n²) and times out on long bot sessions. Data engineers meet it in sessionisation, deduplication windows and 'longest streak of distinct values' metrics. 'Longest Substring Without Repeating Characters' is tagged for data engineers at Apple, Salesforce, Microsoft, PayPal and JPMorgan Chase on Exponent.
longest_unique_run(pages: list[str]) -> dictSubmit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.