Skip to main content
General

Understanding time complexity - a beginner's guide

Lakshay DesaiLakshay Desai
7/2/2025
00

I see many beginners struggle with time complexity so here's a simple guide:

O(1) - Constant: HashMap lookup, array access by index
O(log n) - Logarithmic: Binary search, balanced BST operations
O(n) - Linear: Single loop through array
O(n log n) - Merge sort, heap sort
O(n²) - Quadratic: Nested loops (brute force)
O(2ⁿ) - Exponential: Recursive solutions without memoization

Quick rules:

  • n ≤ 20 → O(2ⁿ) is fine
  • n ≤ 1000 → O(n²) works
  • n ≤ 10⁶ → Need O(n log n) or better
  • n ≤ 10⁸ → Need O(n)

Hope this helps someone!


time-complexitybeginnertutorialdsa

Comments (6)

Sign in to join the discussion.
Amit Rao
Amit Rao11 months ago

I think the rule of thumb for n ≤ 20 with O(2ⁿ) might be a bit too generous. I tried a backtracking puzzle and it struggled even at n = 15. Anyone else faced similar issues?

Ritesh Bajaj
Ritesh Bajaj8/11/2025

For O(n log n), you mentioned merge sort and heap sort. Would you say quicksort generally fits here too, even though it's worst case O(n²)?

Viktor Schneider
Viktor Schneider8/7/2025

This is a really useful breakdown for beginners! Quick question: is it correct that all operations in a HashMap, like put and get, are O(1)? I've read that it can get worse depending on hashing collisions.

Ritesh Pandey
Ritesh Pandey7/22/2025

I've always found exponential time complexity examples tough to grasp. Any simpler examples than the famous Fibonacci recursive method?

Sachin Ahuja
Sachin Ahuja7/11/2025

Thanks for the guide! Isn't it also critical to know space complexity when analyzing algorithms? Sometimes it feels equally important.

Sachin Ahuja
Sachin Ahuja7/8/2025

How do you practically determine the time complexity of a given code snippet? Are there tools or just lots of practice?