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
A table's file index lists one partition key per data file, sorted ascending, so every file for the same date sits in one contiguous run. A query engine pruning files for WHERE event_date = '2026-03-01' needs the first and last position of that date in the index, to read exactly that slice of files and nothing around it.
Write key_range(index_keys, key). It takes index_keys, a list of date strings like "2026-03-01" sorted ascending with repeats next to each other, and key, one date string. It returns a two-element list [first_index, last_index], or [-1, -1] when the key is not in the index.
Function to write
key_range(index_keys: list[str], key: str) -> list[int]A two-element list [first_index, last_index] of the key, or [-1, -1] when it is absent.
How to approach it
A match is not the end of the search. Remember it, then keep halving toward the side you want, once to the left and once to the right.
Sample cases
+ 3 held back until you submit
a date spread over several files
2026-03-01 fills indexes 2 to 5; stopping at the first match found returns index 3 instead.
Input
Argument 1
[
'2026-02-27',
'2026-02-28',
'2026-03-01',
'2026-03-01',
'2026-03-01',
'2026-03-01',
'2026-03-02',
'2026-03-03'
]Argument 2
'2026-03-01'Returns
[
2,
5
]a date the index does not hold
The key falls between two stored dates, so the answer is [-1, -1].
Input
Argument 1
[
'2026-03-01',
'2026-03-01',
'2026-03-03',
'2026-03-04'
]Argument 2
'2026-03-02'Returns
[
-1,
-1
]an empty index
No files at all returns [-1, -1] rather than raising on an empty list.
Input
Argument 1
[] (empty list)
Argument 2
'2026-03-01'Returns
[
-1,
-1
]the key fills the whole index
Every entry matches, so the range is [0, 4], the two ends of the list.
Input
Argument 1
[
'2026-03-05',
'2026-03-05',
'2026-03-05',
'2026-03-05',
'2026-03-05'
]Argument 2
'2026-03-05'Returns
[
0,
4
]Constraints
index_keys is sorted ascending. Dates are ISO YYYY-MM-DD strings, so ordinary string comparison orders them correctly.key and the index of the last copy. Both are 0-based.[2, 2].[-1, -1]. So does an empty index_keys.Worked example
Take ["2026-02-27", "2026-02-28", "2026-03-01", "2026-03-01", "2026-03-01", "2026-03-01", "2026-03-02", "2026-03-03"] and key 2026-03-01.
Leftmost search: low 0, high 7. Middle 3 is 2026-03-01, a match. Record 3 and keep looking left, so high becomes 2. Middle 1 is 2026-02-28, too small, so low becomes 2. Middle 2 is a match. Record 2, high becomes 1, and the loop ends. First is 2.
Rightmost search: low 0, high 7. Middle 3 is a match. Record 3 and keep looking right, so low becomes 4. Middle 5 is a match. Record 5, low becomes 6. Middle 6 is 2026-03-02, too big, so high becomes 5, and the loop ends. Last is 5.
The answer is [2, 5]. The trap is the textbook binary search that returns as soon as it sees the key. Its first probe lands on index 3, the middle of the run, so it returns [3, 3]. On the index of five 2026-03-05 entries it returns [2, 2] instead of [0, 4].
What this tests
Binary search that keeps going after a match: record the candidate, then narrow toward one edge. This is LeetCode 'Find First and Last Position of Element in Sorted Array' in data clothing. The target is O(log n) time and O(1) space, with two searches. Finding one match and walking outward passes small samples and degrades to O(n) when one key dominates the index. Data engineers meet this in file and partition pruning, in slicing a sorted Kafka offset index, and in range lookups over sorted keys in a merge join. This exact problem has been reported by an Atlassian data engineering candidate.
key_range(index_keys: list[str], key: str) -> list[int]Submit 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.