Question presented to candidate:
"Explain how seq[start:stop:step] works in Python. What do s[::-1], s[-3:] and s[4:1:-1] give you, and what happens when you assign to a slice?"
What a strong answer should cover:
- The range is half-open:
startis included,stopis excluded, sos[:k] + s[k:] == sandlen(s[a:b]) == b - afor in-range bounds. - Omitted parts have defaults, negative indices count from the end, and a negative
stepwalks backwards with the defaults flipped (s[::-1]reverses). - Slicing clamps out-of-range bounds instead of raising
IndexError; only a step of 0 is an error (ValueError). - On lists a slice is a new, shallow copy (
a[:]), and slice assignment /del a[i:j]can replace, insert or remove regions; extended slices (step != 1) require an equal-size replacement. a[i:j:k]is sugar fora[slice(i, j, k)]; your own__getitem__receives thesliceobject, andslice.indices(n)shows the clamped values.
Code / implementation expected: Optional β predicting the output of a few slices on a short string or list is the usual check.