Data Structures & Algorithms Big O & Complexity
💡
Exercise 5

Analyze This! 20 XP Easy

Ctrl+Enter Run Ctrl+S Save

Time to test your Big O skills! In this challenge, you'll analyze multiple code snippets and determine both their time AND space complexity.

Quick review:

  • Count the loops: 1 loop = O(n), nested loops = O(n²)
  • Halving = O(log n), sorting = O(n log n)
  • Extra variables only = O(1) space, new list = O(n) space
  • Always take the dominant (biggest) term

Analyze these carefully:

# Snippet A def snippet_a(arr): seen = set() for num in arr: if num in seen: return True seen.add(num) return False
# Snippet B def snippet_b(n): if n <= 1: return n return snippet_b(n-1) + snippet_b(n-2)
# Snippet C def snippet_c(arr): arr.sort() return arr[0]
📋 Instructions
For each snippet (A, B, C), determine the **time** and **space** complexity. Print your analysis in this exact format: ``` A: Time=O(n) Space=O(n) B: Time=O(2^n) Space=O(n) C: Time=O(n log n) Space=O(1) ``` **Hint about Snippet B:** It's recursive Fibonacci — each call branches into 2 more calls. The call stack depth is n.
A: The loop runs n times (O(n) time) and the set can hold up to n items (O(n) space). B: Recursive Fibonacci has exponential time O(2^n) and the call stack goes n deep O(n) space. C: Python's sort is O(n log n) time and sorts in-place so O(1) extra space.
🧪 Test Cases
Input
"A: Time= Space="
Expected
A: Time=O(n) Space=O(n)
Test case 1
Input
"B: Time= Space="
Expected
B: Time=O(2^n) Space=O(n)
Test case 2
Input
"C: Time= Space="
Expected
C: Time=O(n log n) Space=O(1)
Test case 3
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.