Task Assignment Implementation in Rust
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 Rust as in every other language.
- ✓Vec<usize> for the index array, slice::sort_unstable_by_key for the keyed sort, and slice::sort_by_cached_key as the alternative when the key function itself is expensive enough to be worth computing once into a buffer rather than re-running it on every comparison.
- ✓Sort an index Vec via sort_unstable_by_key(|&i| tasks[i]) rather than zipping tasks into (value, index) pairs; it avoids allocating n tuple wrappers, leaves tasks itself untouched for anyone else still holding a reference to it, and is the standard, well-worn pattern for exactly this "sort by a derived key, keep the original" shape.
- ✓The Rust trap: hi is a usize, and the loop decrements it every iteration while walking inward: hi -= 1 when hi is already 0 underflows.
- ✓sort_unstable_by_key re-reads tasks[i] on every comparison inside the closure, which is fine since the key here is a single array lookup.
What Task Assignment actually is
Task Assignment in Rust: what you actually reach for
Rust implementation
fn task_assignment(k: usize, tasks: &[i32]) -> Vec<(usize, usize)> {
let mut indices: Vec<usize> = (0..tasks.len()).collect();
indices.sort_unstable_by_key(|&i| tasks[i]); // sorts the INDEX vec, tasks itself is untouched
let mut pairs = Vec::with_capacity(k);
let mut lo = 0usize;
let mut hi = indices.len() - 1;
for _ in 0..k {
pairs.push((indices[lo], indices[hi]));
lo += 1;
// hi is unsigned: guard before subtracting or the last iteration underflows
if hi > 0 {
hi -= 1;
}
}
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 Rust-specific trap
Performance in Rust, beyond the O(n log n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Rust type or library should I use for Task Assignment?
Vec<usize> for the index array, slice::sort_unstable_by_key for the keyed sort, and slice::sort_by_cached_key as the alternative when the key function itself is expensive enough to be worth computing once into a buffer rather than re-running it on every comparison.
What is the most common Rust mistake when implementing Task Assignment?
hi is a usize, and the loop decrements it every iteration while walking inward: hi -= 1 when hi is already 0 underflows. Debug builds panic on that overflow; a release build compiled with the default overflow-checks = false wraps silently to usize::MAX, and the next indices[hi] then panics with an out-of-bounds index instead of the real error.
How does Task Assignment actually perform in Rust?
sort_unstable_by_key re-reads tasks[i] on every comparison inside the closure, which is fine since the key here is a single array lookup. sort_by_cached_key exists for when the key computation itself is expensive enough to be worth precomputing once into an auxiliary O(n) buffer, and reaching for it here would be pure overhead for a key this cheap. 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 GitHub Copilot 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.