Skip to content

Time & Space Complexity Basics: Big O Notation Explained for DSA

What is Time Complexity and Why is it Needed?

  • Time complexity is not the actual time taken by code to run, as this varies between machines (e.g., old Windows vs. new MacBook).
  • It is defined as the rate at which time taken increases with respect to the input size.
  • In interviews, your code is judged by its time and space complexity using Understanding Time and Space Complexity: A Comprehensive Guide to Big O Notation, not by running it on a specific machine.

Big O Notation and the Three Rules

Big O notation (written as O(...)) is used to express time complexity. The number of operations is what goes inside the parentheses, but three rules simplify the process:

1. Always Consider the Worst-Case Scenario

  • Best case: The minimum time, e.g., a grade code (if-else) that finds a match in the first condition.
  • Average case: The median time.
  • Worst case: The maximum time, e.g., the code runs through all conditions before finding a match. This is what matters for scalability.

2. Avoid Constants

  • Constants become insignificant for large input sizes. For example, if the complexity is 4n3 + 3n2 + 8, and n = 105, the +8 is negligible.
  • In code, a single operation like int x = 2; is a constant and is ignored compared to loop iterations.

3. Avoid Lower-Order Values

  • Lower-order terms (like + 3n2 in a 4n3 expression) are dropped because they don't affect the growth rate significantly for large n.

How to Compute Time Complexity: Examples

Example 1: Nested Loop (Both loops run n times)

for(i=0; i<n; i++) {
  for(j=0; j<n; j++) {
    // constant time operation
  }
}
  • Outer loop runs n times.
  • For each outer iteration, the inner loop runs n times.
  • Total iterations = n * n = n2 → Time Complexity = O(n2)

Example 2: Inner Loop Depends on Outer Loop Counter

for(i=0; i<n; i++) {
  for(j=0; j<=i; j++) {
    // constant time operation
  }
}
  • When i=0: inner loop runs 1 time.
  • When i=1: runs 2 times.
  • ... When i=n-1: runs n times.
  • Sum of first n natural numbers: n(n+1)/2 = n2/2 + n/2.
  • After removing constants and lower-order terms → Time Complexity = O(n2) (Exact: O(n2/2 + n/2), but simplified to O(n2)).

Other Notations (For Knowledge, Not Required in Interviews)

  • Big O (O): Upper bound / worst-case complexity.
  • Omega (Ω): Lower bound / best-case complexity.
  • Theta (Θ): Average complexity.

The focus of this course is on using Big O notation for solving coding problems, not on mathematical derivations.

What is Space Complexity?

  • Space complexity = Auxiliary space + Input space, expressed in Big O notation.
  • Auxiliary space: Extra space you create to solve the problem (e.g., a new variable c to store a + b).
  • Input space: Space taken to store the input (e.g., variables a and b).
  • Just like time complexity, space complexity is expressed in Big O, not in KB/MB.

Crucial Rule: Never Modify the Input

  • In interviews, never manipulate the given input data (e.g., storing a + b in b itself).
  • The data might be used elsewhere in a production system.
  • Always use extra variables or arrays. Using O(2n) space instead of O(n) is acceptable.
  • If the interviewer explicitly allows modifying the input, you can do so, but explain why you prefer not to.

Bonus: Competitive Programming Guideline

  • Most coding platforms (LeetCode, Codeforces, GFG) can execute roughly 108 operations per second.
  • If time limit is 1 second, your code's Big O should be ≤ 108 operations.
  • For a 2-second limit, aim for ≤ 2 * 108 operations.
  • For more advanced techniques to optimize your solutions, check out Mastering Two Pointer & Sliding Window Techniques in DSA Interviews.
  • Use this to choose efficient algorithms.

Keep this summary

Save it to LunaNotes and it becomes a real note in your library — editable, searchable, and ready to turn into flashcards or a diagram. Free to start.

Save to LunaNotes

Or summarise for another video.

32K+ people have turned 65K+ videos into notes

“Very useful tool. I'm using it for the last couple weeks and I'm loving it so far. Makes learning on YouTube a great experience.”
Bhai Chrome Web Store review

This summary and transcript were automatically generated using AI with the Free YouTube transcript summary tool by LunaNotes.

Related summaries

Understanding Time and Space Complexity: A Comprehensive Guide to Big O Notation

Understanding Time and Space Complexity: A Comprehensive Guide to Big O Notation

Explore the concepts of time complexity, space complexity, and Big O notation with practical examples.

Algorithm Complexity: Frequency Count Method for Time Space Analysis

Algorithm Complexity: Frequency Count Method for Time Space Analysis

Learn how to analyze algorithm efficiency using the frequency count method. This video demonstrates time and space complexity analysis for sum of array elements, matrix addition, and matrix multiplication algorithms.

Asymptotic Analysis Part 3: Growth Rates & Time Complexity

Asymptotic Analysis Part 3: Growth Rates & Time Complexity

Learn why we use asymptotic complexity instead of exact running times in algorithm analysis. This tutorial explains how time complexity depends on input size and why higher-order terms dominate for large inputs, using clear examples with array shifts and growth rate calculations.

How to Analyze Time Complexity of Nested Loops and Sequences

How to Analyze Time Complexity of Nested Loops and Sequences

This video teaches you how to calculate the time complexity of various loop structures in code, including simple loops, nested loops, and loops with non-standard increments or conditions. Through detailed tracing and analysis, you'll learn to identify Big O notation for O(n), O(n²), and O(√n) complexities, making algorithm analysis straightforward.

How to Write and Analyze Algorithms: Time and Space Criteria

How to Write and Analyze Algorithms: Time and Space Criteria

This tutorial explains how to write algorithms using flexible syntax, emphasizing clarity for human understanding over strict programming rules. It also covers the key criteria for analyzing algorithms—time efficiency and space consumption—with a basic example demonstrating constant time and space complexity.

Found this summary useful?

Take it with you. One click puts it in your own LunaNotes library.

Save to LunaNotes

Start taking better notes today with LunaNotes