Interview concept

What is Big O notation?

Big O notation describes the upper bound on how an algorithm time or memory grows with input size, ignoring constants. It lets you compare algorithms independent of hardware.

Interview concept

How it works

Common classes

From fastest to slowest: O(1), O(log n), O(n), O(n log n), O(n squared), and O(2 to the n).

Time and space

Analyze both runtime and extra memory; a faster algorithm may use more space.

How to reason

Count how work grows with n, keep the dominant term, and drop constants and lower-order terms.

Interview concept

Worked example: three ways to find a duplicate

Interviewers usually want the complexity of every approach you consider, not only the final one. Take an array of n integers and ask whether any value repeats.

Compare every pair

Two nested loops make about n squared over 2 comparisons: O(n squared) time and O(1) extra space.

Sort, then scan

Sorting costs O(n log n), and checking neighbors costs O(n), so the total is O(n log n) time with little extra space for an in-place sort.

Use a hash set

Insert each value and stop at the first one already present: O(n) average time and O(n) extra space.

Say the tradeoff

"The hash set is fastest on average but uses linear memory. If memory is tight, sorting in place is the middle ground."

Interview concept

Responsible use

Use any live AI assistant only where interview, workplace, school, and platform rules allow it. Do not use generated answers to misrepresent your skills, experience, or authorship.

FAQ

Common questions.

Why does Big O matter in interviews?

Interviewers expect you to state and improve the time and space complexity of your solution, not just make it work.

What is a good target complexity?

It depends on the problem, but reducing an O(n squared) brute force to O(n log n) or O(n) is a common goal.