Splice One Sequence Into Another by Index Range
Arrays & HashingAuto-graded · 22 test casesLast updated
Tests careful list slicing and index boundary handling by replacing a contiguous subrange of one sequence with the full contents of a second sequence.
You have two sequences of integers, base and patch, along with two non-negative integers start and end.
Remove every element at positions start through end (inclusive, zero-indexed) from base, then insert all elements of patch in their original order at the position where those elements were removed. Return the resulting sequence.
Rules
- Elements are zero-indexed. Positions
start,start + 1, ...,endare removed frombase. - Every element of
patchis inserted, in order, exactly where the removed block was. The element at positionstart - 1ofbase(when it exists) is immediately followed by the first element ofpatch, and the last element ofpatchis immediately followed by the element at positionend + 1ofbase(when it exists). patchmay be empty ([]); in that case the removed positions are simply deleted and no new elements are added.- When
start = 0, the result begins with the first element ofpatch, or with the element at positionend + 1ofbaseifpatchis empty. - If the removal covers all of
baseandpatchis empty, return[]. - The result is the unique sequence: prefix of
basebeforestart, then all ofpatch, then the suffix ofbaseafterend.
Constraints
1 <= len(base) <= 10^40 <= len(patch) <= 10^40 <= start <= end < len(base)-10^9 <= val <= 10^9for every element in both sequences
Examples
Example 1
Input: splice_sequence([5, 8, 2, 7, 4], [11, 12], 1, 3)
Output: [5, 11, 12, 4]
Example 2
Input: splice_sequence([1, 2, 3], [9, 9], 0, 0)
Output: [9, 9, 2, 3]
Example 3
Input: splice_sequence([1, 2, 3, 4], [], 2, 3)
Output: [1, 2]
Hints
Hint 1
Think of the result as three independent pieces joined together: the part of base before the removed range, the entirety of patch, and the part of base after the removed range. How do you express each piece with a slice?
Hint 2
Python list slicing handles boundary cases automatically. What does base[:0] return? What does base[len(base):] return? Convince yourself these are correct for the edge cases where start is 0 or end is the last index.
Hint 3
A single line combining three slices is all you need. There is no iteration required and no special-casing for an empty patch.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The result is always made up of three non-overlapping pieces drawn from the two input sequences, concatenated in order:
- Prefix — elements of
basefrom index0up to (but not including)start. This isbase[:start]. - Replacement — every element of
patchin its original order. This is justpatch. - Suffix — elements of
basefrom indexend + 1to the end. This isbase[end + 1:].
Joining them gives base[:start] + patch + base[end + 1:].
Why It Is Correct
- Prefix.
base[:start]selects exactly the elements before the removed range. Whenstart = 0this evaluates to[], which is correct because nothing precedes the removal. - Suffix.
base[end + 1:]selects everything after the last removed element. Whenendis the final index ofbase, this evaluates to[], which is correct because nothing follows the removal. - Replacement.
patchis inserted as a unit. Its internal order is never changed, and an emptypatchcontributes nothing, making it a clean deletion. - Constraint
start <= end < len(base)guarantees that the indices are valid and that at least one element is always removed, so no special-casing is needed.
Edge Cases
start = 0: the prefix slice is[], so the result begins withpatch(or the suffix ifpatchis empty).end = len(base) - 1: the suffix slice is[], so the result ends with the last element ofpatch(or the prefix ifpatchis empty).patch = []: becomes a pure deletion; the prefix and suffix are joined directly.- Both of the above together (
start = 0,end = len(base) - 1,patch = []): all three pieces are empty and the result is[].
Complexity
- Time: O(n + m), where n =
len(base)and m =len(patch). Each element is copied exactly once during slicing and concatenation. - Space: O(n + m) for the returned list. No in-place mutation is performed.
You cannot do better than O(m) because every element of patch must appear in the output.
Reference Solution
from typing import List
def splice_sequence(base: List[int], patch: List[int], start: int, end: int) -> List[int]:
# prefix (elements 0..start-1) + replacement + suffix (elements end+1..n-1)
return base[:start] + patch + base[end + 1:]