Skip to main content

Time Complexity · #34 · 2026-05-08

What's the Big-O?

Go ·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.

Directed graph with V vertices and E edges. What is the time complexity?

package main

func topoSort(n int, edges [][]int) []int {
    inDeg := make([]int, n)
    adj := make([][]int, n)
    for _, e := range edges {
        adj[e[0]] = append(adj[e[0]], e[1])
        inDeg[e[1]]++
    }
    queue := []int{}
    for i := 0; i < n; i++ {
        if inDeg[i] == 0 {
            queue = append(queue, i)
        }
    }
    order := []int{}
    for len(queue) > 0 {
        u := queue[0]
        queue = queue[1:]
        order = append(order, u)
        for _, v := range adj[u] {
            inDeg[v]--
            if inDeg[v] == 0 {
                queue = append(queue, v)
            }
        }
    }
    return order
}

Loading your progress...

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