easy +8 pts

Limit and Offset Query

Implement pagination logic to extract a slice from a list with limit and offset.

Imagine you're building a database query helper. Write a function `limit_offset_query(data, limit, offset) -> list` that returns a list of items starting at `offset` and taking at most `limit` items. If `offset` is beyond the length of `data`, return an empty list. If `limit` is negative, treat it as 0. The original `data` list must not be modified.

Constraints

0 <= offset <= 10^6, -10^3 <= limit <= 10^3, len(data) <= 10^4. Time complexity: O(min(limit, n)). Space complexity: O(min(limit, n)).

Example

>>> limit_offset_query([1,2,3,4,5], 2, 1)
[2, 3]
>>> limit_offset_query([1,2,3], 5, 0)
[1, 2, 3]
>>> limit_offset_query([1,2,3], 2, 2)
[3]
>>> limit_offset_query([1,2,3], 2, 5)
[]
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use Python's list slicing with the given offset and offset+limit.
Remember to clamp limit to non-negative.
If offset is larger than the list length, slicing naturally returns an empty list.
Avoid modifying the original by returning a new list.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.