A. The base case condition is correct; no bug here
B. The slices passed to merge overlap and cause incorrect merging
C. The merge function is called with immutable slices, but merged is mutable
D. The base case condition should check if mid == 0, not arr.len() <= 1
Solution
Step 1: Check base case
The condition if arr.len() <= 1 { return; } correctly handles arrays of length 0 or 1.
Step 2: Verify recursive calls
Disjoint mutable slices arr[..mid] and arr[mid..] are sorted independently.
Step 3: Examine merge
Immutable borrows of disjoint slices to merge are temporary; writes to separate merged vec. No overlap or borrow issues.
Final Answer:
The base case condition is correct; no bug here -> Option A
Quick Check:
Disjoint slices + temporary borrows = safe [OK]
Hint: Verify disjoint slices and borrow lifetimes; this is correct [OK]
Common Mistakes:
Ignoring slice borrowing rules in Rust
Confusing base case condition with bug
Assuming immutable slices cause errors with mutable writes
5. Given a large array of integers with many duplicate values, how can you modify the merge step in merge sort to optimize performance by reducing unnecessary copying?
hard
A. Skip copying elements when both halves have equal values during merge
B. Avoid merging duplicates by sorting only unique elements
C. Use a stable merge that avoids copying duplicates by referencing indices
D. Count duplicates and merge them as a single element to reduce size
Solution
Step 1: Understand stable merge importance
Stable merge preserves order of equal elements and can optimize by referencing indices instead of copying repeatedly.
Step 2: Avoid unnecessary copying of duplicates
By using indices and stable merge logic, duplicates are merged efficiently without extra copying.
Final Answer:
Use a stable merge that avoids copying duplicates by referencing indices -> Option C
Quick Check:
Stable merge optimizes duplicate handling [OK]
Hint: Use stable merge to handle duplicates efficiently [OK]
Common Mistakes:
Trying to merge duplicates as single element (loses data)
Skipping copying equal elements breaks stability
Sorting only unique elements changes array content
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.