Competitive Coding
Unit 1: Behaviour Analysis
Master the art of analysing algorithm behaviour โ understand Big-O notation, time & space complexity, and learn to pick the right algorithm for every competitive coding problem.
โฑ๏ธ 5 hrs theory + 3 hrs practice | ๐ฐ Earning Potential: โน5Kโโน20K/month | ๐ 30 MCQs (Bloom's Mapped)
๐ผ Jobs this unlocks: SDE Intern (โน3โ6 LPA) | Competitive Programmer (โน5โ15 LPA) | Algorithm Engineer (โน8โ20 LPA)
Opening Hook โ The Speed That Separates Good From Great
โก Why Does Your Code Take 10 Seconds While Theirs Takes 10 Milliseconds?
When you search for a restaurant on Zomato, the app searches through 10 lakh+ restaurants and returns results in milliseconds. How? They use hashing (O(1) lookup), not a linear scan (O(n)). That single design choice makes the difference between a 0.001-second response and a 10-second freeze.
Google indexes 10 billion+ web pages and returns results in 0.5 seconds. Behind the scenes, algorithms like PageRank, inverted indices, and distributed sorting work together โ each carefully analysed for time and space complexity.
In competitive programming, you have 1โ2 seconds to process up to 10โถ inputs. A brute-force O(nยฒ) solution that works for n=1000 will TLE (Time Limit Exceeded) for n=10โต. Understanding algorithm behaviour isn't optional โ it's the single most important skill that separates a Codeforces Specialist from a Grandmaster.
This chapter teaches you to think like an algorithm analyst. You'll learn to look at any code and instantly know: "This is O(n log n), it'll pass for nโค10โต" or "This is O(nยฒ), I need to optimise."
Learning Outcomes โ Bloom's Taxonomy Mapped
| Bloom's Level | # | Learning Outcome |
|---|---|---|
| ๐ต Remember | 1 | Define Big-O, Big-ฮฉ, and Big-ฮ notation and state their mathematical definitions |
| ๐ต Remember | 2 | List the 7 common complexity classes: O(1), O(log n), O(n), O(n log n), O(nยฒ), O(2โฟ), O(n!) |
| ๐ข Understand | 3 | Explain why worst-case analysis is preferred over average-case in competitive programming |
| ๐ข Understand | 4 | Describe the time-space tradeoff with real examples (HashMap vs binary search) |
| ๐ก Apply | 5 | Calculate time complexity of given code snippets involving loops, recursion, and nested structures |
| ๐ก Apply | 6 | Apply Big-O simplification rules (drop constants, drop lower-order terms) to mathematical expressions |
| ๐ Analyze | 7 | Compare the efficiency of brute-force vs optimised algorithms for the same problem |
| ๐ Analyze | 8 | Analyze nested loop patterns, divide-and-conquer recurrences, and sliding window techniques for complexity |
| ๐ด Evaluate | 9 | Judge which algorithm/data structure is best suited for a problem given its constraints (n โค 10โต vs n โค 10ยณ) |
| ๐ด Evaluate | 10 | Evaluate time-space tradeoff decisions and justify when to use extra memory for faster execution |
| ๐ฃ Create | 11 | Design optimised solutions by selecting appropriate algorithmic patterns based on constraint analysis |
| ๐ฃ Create | 12 | Write formal complexity analysis reports for competitive programming solutions with proof of correctness |
Concept Explanation โ Behaviour Analysis from Scratch
1. Introduction to Limits and Behaviour of Logic
Why does one algorithm solve a problem in 0.01 seconds while another takes 10 seconds? The answer lies in how the number of operations grows as input size increases. This growth behaviour is what we call algorithmic complexity.
๐ฌ The Core Question: Input Size vs Execution Time
Algorithm behaviour analysis is the study of how an algorithm's resource consumption (time and space) changes as the input size (n) grows towards infinity.
Key Insight:We don't measure time in seconds (hardware-dependent). Instead, we count the number of fundamental operations as a function of input size n.
Example:Linear Search checks each element one by one โ for n elements, it does at most n comparisons. Binary Search halves the search space each step โ for n elements, it does at most logโ(n) comparisons. For n = 1,000,000: linear search = 1,000,000 operations. Binary search = 20 operations. That's a 50,000ร difference!
Linear Search โ O(n)
C++ int linearSearch(int arr[], int n, int target) { for (int i = 0; i < n; i++) { // Runs n times in worst case if (arr[i] == target) return i; // 1 comparison per iteration } return -1; }
Binary Search โ O(log n)
Python def binary_search(arr, target): lo, hi = 0, len(arr) - 1 while lo <= hi: # Runs logโ(n) times mid = (lo + hi) // 2 if arr[mid] == target: return mid elif arr[mid] < target: lo = mid + 1 # Eliminate left half else: hi = mid - 1 # Eliminate right half return -1
2. Understanding Taxonomy in Worst Case
Every algorithm has three possible behaviours depending on the input:
| Case | Definition | Linear Search Example |
|---|---|---|
| Best Case | Minimum operations โ the most favourable input | Target is at index 0 โ O(1) |
| Average Case | Expected operations over all possible inputs | Target is somewhere in the middle โ O(n/2) = O(n) |
| Worst Case | Maximum operations โ the most unfavourable input | Target is the last element or absent โ O(n) |
Why do we focus on worst case? In competitive programming, the judge tests your code against the hardest possible input. If your worst case exceeds the time limit, you get TLE โ even if your average case is fast. Worst-case analysis gives you a guarantee.
3. Analysing Effectiveness and Efficiency
An algorithm must be correct (effective) AND fast (efficient). These are independent properties:
| Property | Question | Example |
|---|---|---|
| Effectiveness (Correctness) | Does it produce the right answer? | Bubble Sort correctly sorts any array โ |
| Efficiency | Does it do so within resource limits? | Bubble Sort is O(nยฒ) โ too slow for n > 10โด โ |
A correct but slow algorithm is useless in competitive coding. Bubble Sort correctly sorts an array, but for n = 10โต, it performs 10ยนโฐ operations โ that's ~10 seconds on a modern CPU. Merge Sort gives the same correct result in O(n log n) = ~1.7 million operations โ under 0.01 seconds.
4. Measuring Time Complexity
Big-O Notation describes the upper bound of an algorithm's growth rate. Formally: f(n) = O(g(n)) if there exist constants c > 0 and nโ such that f(n) โค cยทg(n) for all n โฅ nโ.
The 7 Common Complexity Classes
| Complexity | Name | Example | n=10 | n=100 | n=1000 | n=10โต |
|---|---|---|---|---|---|---|
| O(1) | Constant | Array index access | 1 | 1 | 1 | 1 |
| O(log n) | Logarithmic | Binary search | 3 | 7 | 10 | 17 |
| O(n) | Linear | Single loop | 10 | 100 | 1,000 | 100,000 |
| O(n log n) | Linearithmic | Merge sort | 33 | 664 | 9,966 | 1,660,964 |
| O(nยฒ) | Quadratic | Nested loops | 100 | 10,000 | 1,000,000 | 10,000,000,000 |
| O(2โฟ) | Exponential | Subset generation | 1,024 | 1.27ร10ยณโฐ | โ | โ |
| O(n!) | Factorial | Permutations | 3,628,800 | โ | โ | โ |
O(1) โ Constant Time
C int getFirst(int arr[]) { return arr[0]; // Always 1 operation regardless of array size }
O(log n) โ Logarithmic Time
C++ // Binary search halves the problem each step while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) lo = mid + 1; else hi = mid - 1; }
O(n) โ Linear Time
Python def find_max(arr): mx = arr[0] for x in arr: # Visits each element once if x > mx: mx = x return mx
O(nยฒ) โ Quadratic Time
C++ // Bubble Sort โ nested loops for (int i = 0; i < n; i++) for (int j = 0; j < n - i - 1; j++) if (arr[j] > arr[j+1]) swap(arr[j], arr[j+1]);
Simplification Rules
Rule 1 โ Drop constants: O(2n) = O(n), O(5nยฒ) = O(nยฒ)
Rule 2 โ Drop lower-order terms: O(nยฒ + n) = O(nยฒ), O(nยณ + nยฒ + n) = O(nยณ)
Rule 3 โ Different variables stay: O(n + m) stays as O(n + m), NOT O(n)
5. Measuring Space Complexity
Space complexity measures the memory an algorithm uses as a function of input size.
| Term | Meaning | Example |
|---|---|---|
| Auxiliary Space | Extra space used beyond the input | Merge sort uses O(n) auxiliary array |
| Total Space | Input space + Auxiliary space | Merge sort: O(n) input + O(n) aux = O(n) total |
| In-place Algorithm | Uses O(1) auxiliary space | Quicksort (in-place partitioning), Bubble sort |
| Stack Space | Memory used by recursive calls | Recursive binary search: O(log n) stack frames |
Python # O(n) space โ creates a new list def reverse_copy(arr): return arr[::-1] # New list of size n # O(1) space โ reverses in place def reverse_inplace(arr): i, j = 0, len(arr) - 1 while i < j: arr[i], arr[j] = arr[j], arr[i] i += 1; j -= 1
6. Trade-off Concept โ Time vs Space
Often you can make an algorithm faster by using more memory, or use less memory at the cost of speed. This is the time-space tradeoff.
| Approach | Time | Space | Use When |
|---|---|---|---|
| HashMap lookup | O(1) | O(n) | Memory is cheap, speed matters |
| Binary search on sorted array | O(log n) | O(1) | Memory is tight, data is sorted |
| Prefix sum precomputation | O(1) per query | O(n) | Many range-sum queries |
| Recompute each time | O(n) per query | O(1) | Few queries, tight memory |
Prefix Sum Example โ Precompute to Answer Fast
C++ // Without prefix sum: O(n) per query, O(1) space int rangeSum(int arr[], int l, int r) { int sum = 0; for (int i = l; i <= r; i++) sum += arr[i]; return sum; } // With prefix sum: O(1) per query, O(n) space int prefix[N]; void buildPrefix(int arr[], int n) { prefix[0] = arr[0]; for (int i = 1; i < n; i++) prefix[i] = prefix[i-1] + arr[i]; } int querySum(int l, int r) { return prefix[r] - (l > 0 ? prefix[l-1] : 0); }
7. Complexity Analysis of Common Patterns
Pattern 1: Single Loop โ O(n)
C++ int sum = 0; for (int i = 0; i < n; i++) { // Runs n times sum += arr[i]; // O(1) work per iteration } // Total: n ร O(1) = O(n)
Pattern 2: Nested Loops โ O(nยฒ)
Python for i in range(n): # n iterations for j in range(n): # n iterations each print(i, j) # O(1) work # Total: n ร n ร O(1) = O(nยฒ)
Pattern 3: Binary Search โ O(log n)
C++ int lo = 0, hi = n - 1; while (lo <= hi) { // Each iteration halves the range int mid = (lo + hi) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) lo = mid + 1; else hi = mid - 1; } // Range shrinks: n โ n/2 โ n/4 โ ... โ 1 // Steps = logโ(n), so O(log n)
Pattern 4: Two Pointer โ O(n)
Python # Find pair with given sum in sorted array def two_sum_sorted(arr, target): left, right = 0, len(arr) - 1 while left < right: # Each step moves one pointer s = arr[left] + arr[right] if s == target: return (left, right) elif s < target: left += 1 else: right -= 1 return None # Total pointer moves โค n, so O(n)
Pattern 5: Sliding Window โ O(n)
C++ // Maximum sum subarray of size k int maxSumWindow(int arr[], int n, int k) { int windowSum = 0; for (int i = 0; i < k; i++) windowSum += arr[i]; // Initial window int maxSum = windowSum; for (int i = k; i < n; i++) { windowSum += arr[i] - arr[i - k]; // Slide: add right, remove left maxSum = max(maxSum, windowSum); } return maxSum; } // Only one pass through the array โ O(n)
8. Amortized Analysis (Brief)
๐ฆ ArrayList / Vector Dynamic Resizing
When a C++ vector or Python list runs out of space, it doubles its capacity. Copying n elements costs O(n). But this happens only every n insertions.
Insert 1: cost 1. Insert 2: cost 1 + copy 1 = 2. Insert 3: cost 1. Insert 4: cost 1 + copy 3 = 4. Insert 5โ8: cost 1 each. Insert 9: cost 1 + copy 8 = 9...
Total cost for n insertions โ n + n/2 + n/4 + ... โ 2n. Amortized cost per insertion = 2n/n = O(1).
Key Takeaway:Even though individual operations can be expensive (O(n) for resize), the average cost per operation over a sequence is O(1). This is amortized analysis.
9. Competitive Coding Constraints โ The Cheat Sheet
The single most valuable skill in competitive coding: reading the constraints and immediately knowing what complexity you need.
| Constraint (n) | Max Complexity | Algorithm Examples |
|---|---|---|
| n โค 10 | O(n!) or O(2โฟยทn) | Brute-force permutations, backtracking |
| n โค 20 | O(2โฟ) | Bitmask DP, subset enumeration |
| n โค 500 | O(nยณ) | Floyd-Warshall, DP on intervals |
| n โค 5,000 | O(nยฒ) | Bubble sort, 2D DP, brute-force pairs |
| n โค 10โต | O(n log n) | Merge sort, segment trees, binary search |
| n โค 10โถ | O(n) | Linear scan, prefix sum, two pointers |
| n โค 10โธ | O(log n) or O(1) | Binary search, math formula, O(1) lookup |
Learn by Doing โ 3-Tier Lab Structure
๐ข Tier 1 โ GUIDED: Analyse Code Snippets for Big-O
For each code snippet below, determine the time complexity. Answers follow.
Snippet 1
C++ for (int i = 0; i < n; i++) for (int j = i; j < n; j++) cout << arr[j];
Answer: O(nยฒ) โ Inner loop runs n + (n-1) + ... + 1 = n(n+1)/2 โ O(nยฒ)
Snippet 2
C++ for (int i = 1; i < n; i *= 2) cout << i;
Answer: O(log n) โ i doubles each time: 1, 2, 4, 8, ... until n. Number of iterations = logโ(n).
Snippet 3
C++ for (int i = 0; i < n; i++) for (int j = 0; j < 100; j++) sum++;
Answer: O(n) โ Inner loop runs a constant 100 times. Total = 100n = O(n).
Snippet 4
Python def rec(n): if n <= 1: return rec(n // 2) rec(n // 2)
Answer: O(n) โ Recurrence T(n) = 2T(n/2) + O(1). By Master theorem: O(n).
Snippet 5
C++ for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) sum++;
Answer: O(nยณ) โ Three nested loops each running n times.
๐ก Tier 2 โ SEMI-GUIDED: Optimise O(nยฒ) โ O(n log n)
Problem: Count Inversions
Given an array, count pairs (i, j) where i < j but arr[i] > arr[j].
Brute Force โ O(nยฒ):
C++ int count = 0; for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++) if (arr[i] > arr[j]) count++;
Your task: Use a modified Merge Sort to count inversions in O(n log n). Hint: during the merge step, when an element from the right half is placed before elements from the left half, those are inversions.
๐ด Tier 3 โ OPEN CHALLENGE: Codeforces Problems
Solve these problems and write the complexity analysis for each:
- CF 1A โ Theatre Square (Math, O(1)) โ codeforces.com/problemset/problem/1/A
- CF 4A โ Watermelon (Brute force, O(1)) โ codeforces.com/problemset/problem/4/A
- CF 230B โ T-primes (Sieve + binary search, O(nโn)) โ codeforces.com/problemset/problem/230/B
- CF 279B โ Books (Two pointers / sliding window, O(n)) โ codeforces.com/problemset/problem/279/B
- CF 466C โ Number of Ways (Prefix sums, O(n)) โ codeforces.com/problemset/problem/466/C
For each, submit your solution and write: (a) Time complexity, (b) Space complexity, (c) Why this complexity is optimal for the given constraints.
Problem Set
Syntax & Tracing (5 Questions)
E1. What is the time complexity of this code? for(int i=0; i<n; i++) for(int j=0; j<m; j++) sum++;
E2. How many times does the inner statement execute? for(int i=1; i<=n; i*=3) print(i);
E3. Given T(n) = T(n/2) + O(1), what is the complexity? Trace for n = 16.
E4. What is the output and complexity of: for i in range(n): for j in range(i): print("*")
E5. Simplify: O(3nยฒ + 5n + 100 + n log n). Show each step.
Programming Problems (8 Questions)
E6. Write a function to find the second largest element in an array in O(n) time and O(1) space.
E7. Implement binary search iteratively in C. Your solution must be O(log n).
E8. Write a sliding window function to find the maximum sum of k consecutive elements. Must be O(n).
E9. Given a sorted array with duplicates, count occurrences of a target value in O(log n).
E10. Implement Two Sum: given an array and target, find two numbers that add up to the target. Do it in O(n) using a hash map.
E11. Write a recursive Fibonacci function. What is its time complexity? Now write an O(n) iterative version.
E12. Given two sorted arrays of size n, find the median of the combined array in O(log n).
E13. Write a function that checks if a string has all unique characters in O(n) time.
Industry Problems (3 Questions)
E14. Zomato has 10 lakh restaurants. Design a search system that finds a restaurant by name in O(1) average time. What data structure would you use?
E15. PhonePe processes 3,800 UPI transactions/second. Each transaction must be checked against a fraud database of 1 million known patterns. What's the minimum complexity needed?
E16. IRCTC handles 25 million Tatkal requests. Design a system that processes bookings with O(log n) search and O(1) insertion into the waiting list.
Interview Questions โ Asked at Google/Amazon (3 Questions)
๐ข Asked at Google โ SDE-1 Interview
E17. "Given an array of n integers, find the maximum subarray sum. What's the brute force complexity? Can you do better than O(nยฒ)? What about O(n)?" (Kadane's Algorithm)
๐ข Asked at Amazon โ SDE Intern Interview
E18. "You have a sorted rotated array. Find a target element. What's the time complexity of your approach? Can you do it in O(log n)?"
๐ข Asked at Microsoft โ Complexity Analysis Round
E19. "Analyze this code and tell me the exact complexity:" for(i=0;i<n;i++) for(j=1;j<n;j*=2) sum++; "Now what if the inner loop was j<=i instead of j<n?"
MCQ Assessment Bank โ 30 Questions (Bloom's Mapped)
Remember / Identify (Q1โQ5)
Big-O notation describes the:
- Best-case performance
- Average-case performance
- Upper bound of growth rate
- Exact number of operations
Which complexity class represents constant time?
- O(n)
- O(1)
- O(log n)
- O(n!)
O(n log n) is the time complexity of:
- Bubble Sort
- Linear Search
- Merge Sort
- Binary Search
In worst-case analysis, linear search on an array of n elements takes:
- O(1)
- O(log n)
- O(n)
- O(nยฒ)
Which notation describes the lower bound of an algorithm?
- Big-O (O)
- Big-Omega (ฮฉ)
- Big-Theta (ฮ)
- Small-o (o)
Understand / Explain (Q6โQ10)
Why is worst-case analysis preferred over average-case in competitive programming?
- It's easier to calculate
- It provides a guarantee โ judges test worst inputs
- Average case is always the same as worst case
- Best case is too optimistic
What does it mean when we say O(n/2) = O(n)?
- They take the same time
- Constants are dropped in asymptotic notation
- n/2 and n are the same number
- The notation is wrong
Why is binary search O(log n) instead of O(n)?
- It skips elements randomly
- It halves the search space each step
- It uses recursion
- It works only on small arrays
What is the time-space tradeoff?
- Faster algorithms always use more space
- Using more memory can reduce time, and vice versa
- Time and space are always equal
- You can't reduce both time and space
An algorithm is "correct but inefficient." What does this mean?
- It gives wrong answers quickly
- It gives right answers but takes too long for large inputs
- It uses too little memory
- It can't handle edge cases
Apply / Calculate (Q11โQ15)
What is the time complexity? for(i=0;i<n;i++) for(j=0;j<n;j++) a[i][j]=0;
- O(n)
- O(n log n)
- O(nยฒ)
- O(nยณ)
Simplify: O(5nยณ + 3nยฒ + 2n + 100)
- O(5nยณ)
- O(nยณ + nยฒ)
- O(nยณ)
- O(nยฒ + n)
What is the complexity? for(i=1;i<=n;i*=2) print(i);
- O(n)
- O(n/2)
- O(log n)
- O(nยฒ)
What is the complexity? for(i=0;i<n;i++) for(j=1;j<n;j*=2) sum++;
- O(nยฒ)
- O(n log n)
- O(n)
- O(log n)
Recursive function: T(n) = 2T(n/2) + O(n). What is the complexity?
- O(n)
- O(n log n)
- O(nยฒ)
- O(log n)
Analyze / Compare (Q16โQ20)
Which grows faster as n โ โ?
- n log n grows faster than nยฒ
- nยฒ grows faster than n log n
- They grow at the same rate
- Depends on the value of n
Compare: Algorithm A is O(100n), Algorithm B is O(nยฒ). For which n is A faster?
- Always
- n > 100
- n < 100
- Never
Code: for(i=0;i<n;i++) for(j=0;j<i;j++) sum++; โ Total operations?
- nยฒ
- n(n-1)/2
- n log n
- n
HashMap lookup is O(1) average but O(n) worst case. When does worst case occur?
- When the key doesn't exist
- When all keys hash to the same bucket (collision)
- When the HashMap is empty
- When using string keys
What is the space complexity of recursive Fibonacci fib(n) = fib(n-1) + fib(n-2)?
- O(1)
- O(n)
- O(2โฟ)
- O(nยฒ)
Evaluate / Judge (Q21โQ25)
For n โค 10โต, which approach is best for sorting?
- Bubble Sort O(nยฒ)
- Merge Sort O(n log n)
- Permutation sort O(n!)
- Selection Sort O(nยฒ)
Problem: Find a pair with target sum. Array is unsorted, n โค 10โถ. Best approach?
- Sort + binary search โ O(n log n)
- Brute force nested loops โ O(nยฒ)
- HashMap โ O(n) time, O(n) space
- Recursive backtracking โ O(2โฟ)
When should you prefer O(nยฒ) over O(n log n)?
- Never
- When n is very small (n โค 50) and O(nยฒ) has smaller constants
- When memory is unlimited
- When the data is already sorted
A solution passes for n=10ยณ but TLEs for n=10โต. What should you do?
- Submit again โ maybe the judge was slow
- Optimize constants by using faster I/O
- Reduce complexity from O(nยฒ) to O(n log n) or better
- Increase the time limit
Prefix sum uses O(n) extra space to answer range queries in O(1). Is the tradeoff worth it for 10โถ queries?
- No โ O(n) space is too much
- Yes โ saving O(n) per query ร 10โถ queries outweighs the O(n) space cost
- Only if n < 100
- It depends on the CPU speed
Create / Design (Q26โQ30)
To check if any two elements in an array are equal, the most efficient approach is:
- Sort then scan โ O(n log n)
- HashSet โ O(n)
- Nested loops โ O(nยฒ)
- Binary search โ O(n log n)
Design a solution for: "Given n โค 10โต, find the k-th smallest element." Best complexity?
- Sort entire array โ O(n log n)
- Min-heap of size k โ O(n log k)
- Quickselect โ O(n) average
- Brute force โ O(n ร k)
Problem: Find max sum subarray. Which algorithmic pattern gives O(n)?
- Binary search
- Dynamic programming (Kadane's)
- Divide and conquer
- Greedy with sorting
To find the longest substring without repeating characters in O(n), you would use:
- Brute force with nested loops
- Sliding window with a HashSet
- Dynamic programming table
- Sorting the string first
You need to support: insert O(log n), delete O(log n), and find-min O(1). Which data structure?
- Array
- Linked list
- Min-Heap (priority queue)
- Stack
Short Answer Questions (8)
SA1. What is Big-O notation? Why do we use it?
Model Answer: Big-O notation describes the upper bound of an algorithm's growth rate as input size approaches infinity. We use it because it's hardware-independent โ instead of measuring seconds (which vary by CPU), we measure how operations scale with input size. O(nยฒ) means operations grow quadratically, regardless of the computer.
SA2. Explain with an example why we focus on worst-case analysis.
Model Answer: In competitive coding, the judge tests against the hardest possible inputs. For linear search, best case is O(1) (element at index 0), but worst case is O(n) (element absent). If we plan for average case, our solution might TLE on worst-case test data. Worst case gives a guarantee of maximum execution time.
SA3. Differentiate between auxiliary space and total space complexity.
Model Answer: Auxiliary space is the extra memory beyond the input (e.g., temp variables, auxiliary arrays). Total space = input space + auxiliary space. Example: Merge sort has O(n) total space (n for input + n for auxiliary array) but O(n) auxiliary space. In-place quicksort has O(log n) auxiliary space (recursion stack) and O(n) total space.
SA4. What are the two Big-O simplification rules? Give examples.
Model Answer: Rule 1: Drop constants โ O(3n) = O(n), O(100nยฒ) = O(nยฒ). Rule 2: Drop lower-order terms โ O(nยฒ + n) = O(nยฒ), O(nยณ + nยฒ + n) = O(nยณ). The dominant term determines growth. For n = 10โถ: nยฒ = 10ยนยฒ while n = 10โถ, so the n term is negligible.
SA5. Explain the time-space tradeoff with a real example.
Model Answer: You can often trade memory for speed. Example: Finding if a value exists in an unsorted array โ linear scan is O(n) time, O(1) space. Building a HashSet first is O(n) time + O(n) space, but subsequent lookups are O(1). If you have 1 million lookups, HashSet saves 10โถ ร n operations at the cost of O(n) extra memory.
SA6. Why is O(n log n) considered the "sweet spot" for sorting?
Model Answer: It's proven that comparison-based sorting cannot be faster than O(n log n) in the worst case (information-theoretic lower bound). Algorithms like Merge Sort and Heap Sort achieve this bound. It's fast enough for n โค 10โถ (โ20 million operations), making it practical for almost all competitive programming constraints.
SA7. What is amortized analysis? Give an example.
Model Answer: Amortized analysis calculates the average cost per operation over a sequence of operations. Example: Dynamic array (vector) resizing โ individual append can cost O(n) during resize, but resize happens only every n insertions. Total cost for n appends โ 2n. Amortized cost = 2n/n = O(1) per append.
SA8. How do you read competitive programming constraints to determine required complexity?
Model Answer: Use the rule: ~10โธ operations/second. For time limit T seconds, max operations = T ร 10โธ. Match n to complexity: n โค 10โต โ need O(n log n). n โค 5000 โ O(nยฒ) works. n โค 20 โ O(2โฟ) ok. n โค 10 โ O(n!) feasible. Always check constraints FIRST before coding.
Long Answer Questions (3)
LA1. Explain all 7 common complexity classes with examples, code, and a growth comparison table. (15 marks)
Model Answer: The seven classes are O(1), O(log n), O(n), O(n log n), O(nยฒ), O(2โฟ), O(n!). O(1) = array indexing โ constant regardless of size. O(log n) = binary search โ halves problem each step. O(n) = linear scan โ visits each element once. O(n log n) = merge sort โ divide in log n levels, merge n elements per level. O(nยฒ) = bubble sort โ nested loops comparing all pairs. O(2โฟ) = subset generation โ each element is either included or not. O(n!) = permutation generation โ n choices for first, n-1 for second, etc. Growth comparison at n=1000: O(1)=1, O(log n)=10, O(n)=1000, O(n log n)=10000, O(nยฒ)=10โถ, O(2โฟ)=โ, O(n!)=โ. Only O(1) through O(n log n) are practical for large inputs.
LA2. Analyze the complexity of the following 5 algorithmic patterns with code examples: single loop, nested loop, binary search, two pointers, sliding window. (15 marks)
Model Answer: (1) Single loop: iterates n times with O(1) work each โ O(n). Example: finding maximum in array. (2) Nested loop: outer runs n, inner runs n โ nรn = O(nยฒ). Example: checking all pairs for duplicates. (3) Binary search: halves search space each step โ O(log n). Requires sorted data. Example: finding target in sorted array. (4) Two pointers: two indices moving inward, total n moves โ O(n). Example: finding pair with target sum in sorted array. (5) Sliding window: window of fixed/variable size moves right, each element enters and leaves once โ O(n). Example: maximum sum subarray of size k. Key insight: patterns 4 and 5 convert O(nยฒ) brute force to O(n) by maintaining state incrementally.
LA3. Explain the constraint-to-complexity mapping technique used in competitive programming. How does reading constraints help you choose the right algorithm? Provide a detailed table and 3 problem examples. (15 marks)
Model Answer: Modern computers process ~10โธ operations/second. For a 1-second time limit, your algorithm must do โค 10โธ operations. Mapping: nโค10โO(n!), nโค20โO(2โฟ), nโค500โO(nยณ), nโค5000โO(nยฒ), nโค10โตโO(n log n), nโค10โถโO(n), nโค10โธโO(log n). Example 1: "Sort n numbers, nโค10โต" โ must use O(n log n) sort (merge/quick), not O(nยฒ) bubble sort. Example 2: "Find subset with target sum, nโค20" โ O(2ยฒโฐ) = ~10โถ, bitmask DP feasible. Example 3: "Given nโค10โถ numbers, find two with sum = target" โ need O(n) solution, use HashMap. This technique is the most valuable competitive programming skill โ read constraints first, determine complexity ceiling, then pick algorithm.
Lab Programs
All lab exercises for this unit are integrated into Section D โ 3-Tier Labs. The tiered structure provides guided (Tier 1), semi-guided (Tier 2), and open challenge (Tier 3) lab experiences covering Big-O analysis, algorithm optimization, and Codeforces problem solving.
Industry Spotlight โ A Day in the Life
๐จโ๐ป Arjun Mehta, 24 โ SDE-1 at Google India, Bangalore
Background: B.Tech from NIT Warangal. Started competitive programming in 2nd year. Reached Codeforces Expert (1600+) and solved 500+ LeetCode problems. Got Google interview through competitive coding contest โ they noticed his Codeforces profile.
A Typical Day:
9:30 AM โ Check team's code review queue. Review a colleague's search ranking algorithm โ "This nested loop is O(nยฒ), we can use a HashMap to make it O(n)."
10:30 AM โ Work on optimising Google Maps' route suggestion engine. Current algorithm is O(Vยฒ) for V vertices โ implementing Dijkstra with min-heap for O((V+E) log V).
12:00 PM โ Team design review meeting. Present complexity analysis of three proposed approaches. Team picks the O(n log n) solution over the O(nยฒ) one.
2:00 PM โ Lunch at Google cafeteria. Discuss the latest Codeforces Div 2 round with colleagues.
3:00 PM โ Write unit tests and benchmark the optimised algorithm. Confirm 50x speedup for production traffic.
5:00 PM โ 1 hour of LeetCode practice โ keeping competitive programming skills sharp.
6:30 PM โ Mentor a junior intern on time complexity analysis.
| Detail | Info |
|---|---|
| Tools Used Daily | C++, Python, Algorithm Design, Code Review, Profiling Tools |
| Entry Salary (SDE-1) | โน25โ45 LPA (Google India) |
| Key Skill | Complexity analysis โ every code review involves Big-O discussion |
| Competitive Coding Profile | Codeforces Expert, 500+ LeetCode, ACM ICPC regionalist |
| Companies Hiring CP Coders | Google, Amazon, Microsoft, Flipkart, DE Shaw, Tower Research, Directi |
Earn With It โ Freelance & Income Roadmap
๐ฐ Your Earning Path After This Chapter
Portfolio Piece: A complexity analysis report comparing brute-force vs optimised solutions for 5 problems โ demonstrates analytical thinking to employers.
Competitive Programming Coaching: Teach juniors Big-O analysis, constraint mapping, and pattern recognition. Charge โน500โโน1,500/hr on platforms like Codementor, Preply, or local tutoring.
Problem Setting: Create algorithmic problems for college contests or platforms. โน500โโน2,000 per problem with test cases and editorials.
| Platform | Best For | Typical Rate |
|---|---|---|
| Codementor | 1-on-1 competitive programming coaching | โน500โโน1,500/hr ($7โ$20/hr) |
| Preply | Algorithm tutoring for interview prep | โน400โโน1,200/hr |
| Unstop | Problem setting for Indian college contests | โน500โโน2,000/problem |
| Topmate | Mentorship sessions for DSA/CP | โน300โโน1,000/session |
| LinkedIn/WhatsApp | Direct coaching to juniors at your college | โน300โโน800/hr |
Chapter Summary & Complexity Cheat Sheet
๐ Key Takeaways
- Algorithm behaviour is measured by how operations grow with input size, not by clock time
- Big-O notation gives the upper bound (worst case) of growth rate
- Worst case is preferred because competitive judges test hardest inputs
- Always drop constants and drop lower-order terms when simplifying
- Time-space tradeoff: use more memory to get faster execution when needed
- The 5 key patterns: single loop O(n), nested O(nยฒ), binary search O(log n), two-pointer O(n), sliding window O(n)
- Read constraints first โ they tell you the maximum complexity your solution can have
Complexity Cheat Sheet
| Complexity | Name | Example | Ops at n=10โถ | Feasible? |
|---|---|---|---|---|
| O(1) | Constant | Array index, hash lookup | 1 | โ Always |
| O(log n) | Logarithmic | Binary search | 20 | โ Always |
| O(n) | Linear | Single scan, prefix sum | 10โถ | โ <0.01s |
| O(n log n) | Linearithmic | Merge sort, sort+search | 2ร10โท | โ ~0.2s |
| O(nยฒ) | Quadratic | Nested loops, bubble sort | 10ยนยฒ | โ ~3hrs |
| O(2โฟ) | Exponential | Subset generation | โ | โ Only nโค25 |
| O(n!) | Factorial | Permutations | โ | โ Only nโค12 |
Checkpoint โ Self-Assessment
| Skill | Tool/Method | Portfolio Output | Can You Earn? |
|---|---|---|---|
| Big-O Notation | Mathematical analysis | โ | โ Yes โ interview-ready skill |
| Complexity Identification | Code pattern recognition | 5 analysed code snippets | โ Yes โ core interview skill |
| Constraint Mapping | Constraint โ complexity table | Cheat sheet reference | โ Yes โ contest-ready skill |
| Algorithm Optimisation | Pattern application | O(nยฒ) โ O(n log n) solution | โ Yes โ high-value skill |
| Time-Space Tradeoff | HashMap, prefix sum | Tradeoff analysis document | โ Yes โ system design skill |
| Common Patterns | Two-pointer, sliding window, binary search | 5 pattern implementations | โ Yes โ covers 80% of problems |
| Competitive Coding | Codeforces, LeetCode | 5 solved problems with analysis | โ Yes โ โน500โโน1,500/hr coaching |
โ Unit 1 complete. MCQs: 30. Ready for Unit 2!
[QR: Link to EduArtha video tutorial โ Behaviour Analysis & Complexity]