Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Divide and Conquer proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Split one input into smaller instances, solve each with the same return contract, then combine their complete results. Count split, merge, copying and stack work rather than assuming every recursive tree is linear.
2 practice exercises5 interview problems
Recognize it when A range can be partitioned into independently solvable smaller ranges.
Checking your account…
Divide and conquer works when a problem can be partitioned into smaller independent instances of the same contract. The base case must be complete, and every recursive call must receive a strictly smaller input.
Before recursing, say what each call returns. The merge step may interleave sorted halves, combine counts, or attach reconstructed subtrees, but it must use only the promised sub-results and preserve every original element.
def divide_merge_trace(length):
ranges = []
def visit(left, right):
if left >= right:
return
ranges.append([left, right])
if right - left == 1:
return
mid = (left + right) // 2
visit(left, mid)
visit(mid, right)
visit(0, length)
return rangesImplement divide_merge_trace(length). Return all non-empty half-open ranges [left, right] visited by a balanced root-left-right split, stopping at ranges of length one.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
def divide_merge_trace(length):
ranges = []
def visit(left, right):
if left >= right:
return
ranges.append([left, right])
if right - left == 1:
return
mid = (left + right) // 2
visit(left, mid)
visit(mid, right)
visit(0, length)
return rangesThe existing range Lab traces control flow rather than sorting values. Predict root-left-right order for an odd length; equal-sized halves are not required for correctness.
| Step | Executed statement | State | Why |
|---|---|---|---|
| Record parent | ranges.append([left,right]);mid=(left+right)//2 |
parent=[0,3], mid=1 |
Floor division permits unequal child lengths while both are smaller. |
| Solve left base | visit(left,mid);ranges.append([left,right]);if right-left==1:return |
left child [0,1] recorded once |
A singleton needs no further split. |
| Split right child | visit(mid,right);ranges.append([left,right]);mid=(left+right)//2 |
right child=[1,3], its mid=2 |
The child retains absolute indexes in the original input. |
| Finish leaves | visit(left,mid);visit(mid,right) |
leaves [1,2] and [2,3] after their visits |
The three leaves cover indexes 0,1,2 exactly once. |
| Empty call | if left>=right:return |
ranges=[] |
An empty range is a complete base case with no recorded node. |
| Output | |
|---|---|
print(divide_merge_trace(3)) |
[[0, 3], [0, 1], [1, 3], [1, 2], [2, 3]] |
print(divide_merge_trace(0)) |
[] |
To transfer to merge sort, add a linear combine operation after each pair of child returns. That extra work changes the linear trace cost to n log n. Tree reconstruction has a different partition rule and may be skewed.
def merge_sorted_halves(left, right):
merged = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
merged.append(left[i])
i += 1
else:
merged.append(right[j])
j += 1
merged.extend(left[i:])
merged.extend(right[j:])
return mergedImplement merge_sorted_halves(left, right). Return all values in sorted order without mutating either already-sorted input.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
def merge_sorted_halves(left, right):
merged = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
merged.append(left[i])
i += 1
else:
merged.append(right[j])
j += 1
merged.extend(left[i:])
merged.extend(right[j:])
return mergedWrite a recurrence. Balanced halves with linear merging give T(n) = 2T(n/2) + O(n) = O(n log n). Python slices copy, so pass indexes when extra copying would add hidden time or space. Recursion also consumes stack frames.
Use divide and conquer when subproblems are naturally independent and the combine is clear. Prefer iteration for a simple linear dependency or when Python recursion depth is a risk. Dynamic programming is different: it is valuable when subproblems overlap and should be cached.
Say: “The base case is complete, the split covers the input without overlap, and each recursive call satisfies the same return contract. The merge reconstructs the full answer.” Then solve the recurrence and count stack and slicing costs. Build Tree from Inorder and Postorder(opens in a new tab) is a representative partition-by-index transfer problem.
Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Divide and Conquer proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| Balanced range trace | O(n + 1) | O(n + 1) | One recorded range per recursion-tree node; no merging of values. |
| Merge of two sorted lists | O(a + b) | O(a + b) | Every input value is emitted once; leftover slices also copy. |
| Balanced mergesort | O(n log n) | O(n log n) | Linear combining on each logarithmic level, unlike a range trace. |
O(n) range trace or merged output; balanced recursion frames O(log(n+1)); skewed recursive splits can use O(n) frames.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
[left,mid) and [mid,right) partition a half-open range. Starting the right child at mid-1 overlaps index mid-1.
Prevent it: Use one shared boundary without duplicating its predecessor.
right_child=(mid-1,right)right_child=(mid,right)Where you will hit this: Partition Traversal Ranges(opens in a new tab)
If one input empties first, its partner still contains required values. The main loop alone cannot emit them.
Prevent it: Append both remaining tails after the paired comparison loop.
merged.extend([])merged.extend(left[i:])
merged.extend(right[j:])Where you will hit this: Partition Traversal Ranges(opens in a new tab)
Selecting the smaller first head from an unsorted child can emit 4 before an undiscovered 1. The head is minimal only for a sorted child.
Prevent it: Meet the sorted-input merge contract before comparing heads.
next_value=min(left[0],right[0])next_value=min(sorted(left)[0],sorted(right)[0])Where you will hit this: Build Tree from Inorder and Postorder(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27
python-docs · checked 2026-07-27
python-docs · checked 2026-07-27