Skip to main content

Time Complexity · #33 · 2026-05-07

What's the Big-O?

Go ·Difficulty 2/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.

Matrix is m x n. What is the time complexity?

package main

func spiralOrder(matrix [][]int) []int {
    if len(matrix) == 0 {
        return nil
    }
    m, n := len(matrix), len(matrix[0])
    result := make([]int, 0, m*n)
    top, bottom, left, right := 0, m-1, 0, n-1
    for top <= bottom && left <= right {
        for j := left; j <= right; j++ {
            result = append(result, matrix[top][j])
        }
        top++
        for i := top; i <= bottom; i++ {
            result = append(result, matrix[i][right])
        }
        right--
        if top <= bottom {
            for j := right; j >= left; j-- {
                result = append(result, matrix[bottom][j])
            }
            bottom--
        }
        if left <= right {
            for i := bottom; i >= top; i-- {
                result = append(result, matrix[i][left])
            }
            left++
        }
    }
    return result
}

Loading your progress...

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