Skip to main content

Time Complexity · #38 · 2026-05-12

What's the Big-O?

Rust ·Difficulty 3/3

How to play

Read the code and pick its time complexity from four Big-O choices. Think about loops, recursion, and hidden costs. Press 1–4 or click to answer.

n = nums.len(). What is the time complexity?

use std::collections::BinaryHeap;
use std::cmp::Reverse;

fn top_k(nums: &[i32], k: usize) -> Vec<i32> {
    let mut heap = BinaryHeap::new();
    for &x in nums {
        heap.push(Reverse(x));
        if heap.len() > k {
            heap.pop();
        }
    }
    heap.into_iter().map(|Reverse(x)| x).collect()
}

Loading your progress...

Press 1 through 4, or tap a numbered choice, to answer. Back to hub