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 = n2Time 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.

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.

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.

Comprehensive Overview of Data Structures and Algorithms Using Python

Comprehensive Overview of Data Structures and Algorithms Using Python

This video provides an in-depth exploration of data structures and algorithms using Python, covering essential topics such as linked lists, stacks, queues, and sorting algorithms. The session includes practical coding examples, theoretical explanations, and insights into the efficiency of various algorithms.

Mastering Two Pointer & Sliding Window Techniques in DSA Interviews

Mastering Two Pointer & Sliding Window Techniques in DSA Interviews

This video introduces the fundamental patterns of the two pointer and sliding window algorithms from the A2Z DSA course, focusing on problem-solving strategies to maximize efficiency in coding interviews. It covers four main problem types along with reusable templates, enabling learners to understand and implement optimal solutions to common interview challenges.

Comprehensive Guide to Core Data Structures and Algorithms

Comprehensive Guide to Core Data Structures and Algorithms

Explore foundational concepts in data structures, computational complexity, and advanced algorithms. This guide covers arrays, linked lists, stacks, queues, priority queues, union-find, trees, hash tables, Fenwick trees, suffix arrays, balanced BSTs, and indexed priority queues, with insights on implementation and performance optimization.

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