- Big-O is about how time and extra memory grow as n grows, not the milliseconds on your laptop.
- O(1), O(n), O(log n) and O(n²) cover most beginner coding rounds in India.
- One cheap loop is usually O(n). Nested loops, or
inon a list, often mean O(n²). - Python dict and set lookups are average O(1). List membership is O(n). append is cheap; insert at 0 is not.
- You cannot pass a typical SDE coding round without this. One honest hour of loop-reading is enough to start.
What is Big-O notation, in one line?
You are outside an OMR office with filter coffee. Your roommate just walked out of a product-company screen looking wrecked. The function ran. The offer did not. The follow-up was not about syntax. It was time complexity. They froze. Big-O is a short label for how time or extra memory grows when the input size n grows. You drop constants and the small terms. You keep the piece that blows up first.
That is Big-O explained simply. Not a stopwatch. A growth story you can say out loud in a round.
Why do we even measure how code grows?
Forty items on a college laptop will never betray you. Four lakh rows in a Flipkart catalogue or an IRCTC-sized list will. Two functions can both be correct and still be different animals at scale.
Interviewers here are not flexing calculus. They want to know you will not drop an O(n²) scan inside a busy path. Growth is the language you share with whoever owns uptime.
If n doubles and your work barely moves, you are safe. If n doubles and your work squares, the hidden test case times out. Correct is not enough. Correct and scalable is the job.
What do O(1), O(n), O(log n) and O(n²) actually mean?
O(1) means constant work. n can be 10 or a crore. Reading nums[0] does not care. An average dict lookup does not care.
O(n) means linear. Double the list, roughly double the work. One pass. Linear search. Sum of an array. If you see a single for and the body is cheap, say O(n).
O(log n) means each step throws away half the problem. Binary search on a sorted list is the one they expect you to name. The log is base 2 in practice.
O(n²) means nested loops over n. Compare every pair. Fine at n=100. Pain at n=10,000. This is the one that fails the hidden test case.
Do not fight over 2n versus n. Both are O(n). O(n² + n) is O(n²). They want the dominant term.
Here is O(1) sitting next to O(n). Same file. Different growth.
def first_user(users):
return users[0]
def find_user(users, name):
for user in users:
if user == name:
return True
return Falsefirst_user does not walk the list. find_user may scan every name. That is O(1) versus O(n) in two functions you can actually run.
Here is O(log n). The list must already be sorted. Empty list returns -1, no drama.
def binary_search(nums, target):
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return mid
if nums[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1And here is O(n²). Every id against every other id. Works. Does not scale.
def has_duplicate_nested(ids):
n = len(ids)
for i in range(n):
for j in range(i + 1, n):
if ids[i] == ids[j]:
return True
return FalseHow do you read a loop and guess the complexity?
Ask one thing. As n grows, how many times does the inner line run?
One loop to n is O(n). Two nested loops to n is O(n²). A while that halves n each time is O(log n). Work that never looks at n is O(1).
The trap is a single for with if x in some_list inside. Membership on a list is O(n), so the function is O(n²). Put those values in a set. You drop to O(n). That swap is a callback moment.
A slice like nums[:] copies n items. Sorting inside a loop is O(n log n) per call. Sort once outside if you can.
Recursion: n calls of cheap work is O(n). n calls of O(n) work is O(n²). Do not say O(n) just because you see one def.
Does memory also have a Big-O?
Yes. Time is one bill. RAM is the other. Extra lists, extra dicts, recursion depth.
A new list the size of the input is O(n) extra space. Two index swaps in place are O(1) extra. They usually ignore the input itself unless they say total space.
Python recursion adds a stack frame per call. A naive walk of n nodes is O(n) space even with no list. Product-company interviewers ask this right after time.
A set of every id you have seen is O(n) extra space. You bought speed with memory. Say the trade out loud.
def has_duplicate_set(ids):
seen = set()
for user_id in ids:
if user_id in seen:
return True
seen.add(user_id)
return FalseSame problem as the nested version. Time drops to O(n) average. Extra space becomes O(n). That sentence is the interview.
What do lists, dicts and sets actually cost in Python?
This is CPython, the interpreter you run in interviews. Not a textbook machine.
List: get and set by index are O(1). append is amortized O(1). pop from the end is O(1). Insert or pop at index 0 is O(n), because the rest shifts. x in my_list is O(n). A full copy slice is O(n) time and O(n) space.
Dict and set: add, get, delete, and in are average O(1). Worst case can degrade. In a round, say average O(1) unless they push. Keys must be hashable. A list cannot be a key. A tuple of ids can.
A tuple is a frozen list. Index reads are O(1). No append.
heapq push and pop are O(log n). Use that when they want kth largest without a full sort.
collections.deque: append and pop on both ends are O(1). Need a queue? Do not list.pop(0). That O(n) shift is a favourite trap.
Which mistakes get people into trouble?
They see one for and say O(n), while the body scans a list. Nested work hides in helpers.
They write O(3n) and argue about the 3. Drop the constant.
They call a dict O(1) as a worst-case guarantee. Average is the honest word.
They sort inside a loop and forget sort is O(n log n).
They fix time and allocate a huge extra matrix. Space still counts.
They quote best case. Linear search is O(1) if the item sits first. Rounds want worst case unless they say average.
They mix log n with n. Halving is log. Visiting every element is n. A loop 0, 1, 2, ... n is not log.
Where do Indian interviews actually grill you on this?
TCS NQT-style papers often stay at naming the Big-O of a loop. Zoho, Freshworks, Chargebee, and most Bengaluru product companies will make you derive it on a whiteboard.
Flipkart, Amazon, and Google India SDE screens treat this as table stakes. You cannot pass a coding round without it. A focused hour of loop-reading covers most beginner questions.
Service-company interviews in Chennai, Pune, and Hyderabad still use it as a filter. They will not ask for the master theorem. They will paste a nested loop and wait. Mumble, and they move on.
What should you do now?
Open a notebook. Skip another playlist. Pick three functions you already wrote — a lookup like a UPI app finding a user, a report that nested-loops orders, a binary search you copied. Write the Big-O on top in a comment.
Practice task: you get a list of n user ids, duplicates allowed. Return True if any id appears twice. Write two Python versions, nested loop and a set. State time and extra space for both. Run them in your head for n=10 and n=1,00,000. That hour is the round.




Comments (0)
Be the first to comment!