Task Assignment Implementation in Swift
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Task Assignment runs in O(n log n) time and O(n) space, in Swift as in every other language.
- ✓Array.indices (a Range<Int>), Sequence.sorted(by:), and the global zip(_:_:) function are the three pieces; ArraySlice, what prefix and suffix return, shares storage with its parent array rather than copying it.
- ✓Reach for zip() over prefix(k) and suffix(k).reversed() instead of hand-rolled lo/hi pointer bookkeeping; it expresses "walk two ends of a collection toward the middle" directly, and Swift's array slices make prefix and suffix views rather than copies, so there's no extra allocation for expressing it this way.
- ✓The Swift trap: tasks.indices is the half-open range 0..<count, never 0...count. Write the closed-range version by hand instead of reaching for .indices and the range's upper bound becomes count itself, one past the last valid position, which crashes with 'Array index is out of range' the moment it's used.
- ✓prefix and suffix on an Array return ArraySlice, which shares the parent array's storage rather than copying it, and reversed() on that slice is a lazy wrapper, not a new reversed array, so the whole pairing walk after the one sort is O(k) with no additional allocation beyond the single ranked array.
What Task Assignment actually is
Task Assignment in Swift: what you actually reach for
Swift implementation
func taskAssignment(_ k: Int, _ tasks: [Int]) -> [(Int, Int)] {
let ranked = tasks.indices.sorted { tasks[$0] < tasks[$1] } // indices is 0..<count, never 0...count
var pairs: [(Int, Int)] = []
for (shortestIdx, longestIdx) in zip(ranked.prefix(k), ranked.suffix(k).reversed()) {
pairs.append((shortestIdx, longestIdx))
}
return pairs
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Swift-specific trap
Performance in Swift, beyond the O(n log n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Swift type or library should I use for Task Assignment?
Array.indices (a Range<Int>), Sequence.sorted(by:), and the global zip(_:_:) function are the three pieces; ArraySlice, what prefix and suffix return, shares storage with its parent array rather than copying it.
What is the most common Swift mistake when implementing Task Assignment?
tasks.indices is the half-open range 0..<count, never 0...count. Write the closed-range version by hand instead of reaching for .indices and the range's upper bound becomes count itself, one past the last valid position, which crashes with 'Array index is out of range' the moment it's used.
How does Task Assignment actually perform in Swift?
prefix and suffix on an Array return ArraySlice, which shares the parent array's storage rather than copying it, and reversed() on that slice is a lazy wrapper, not a new reversed array, so the whole pairing walk after the one sort is O(k) with no additional allocation beyond the single ranked array. Time complexity is O(n log n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialIf you are a student, see what Replit offers students for free on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.