Swift
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Optionals
-
Section 4: Object-Oriented and Value Types
-
Section 5: Memory Management
-
Section 6: Generics and Error Handling
-
Section 7: Concurrency
-
Section 8: Working with Collections
-
Section 9: Codable and Data Handling
-
Section 10: Protocol-Oriented Programming
-
Section 11: Testing and Tooling
-
Section 12: Practical Projects
-
Section 13: Interview Practice
-
Section 14: More Practice Exercises
-
Section 15: More Standard Library
-
Section 16: Advanced Concurrency
-
Section 17: Foundation Framework Deep Dive
-
Section 18: URLSession and Networking Deep Dive
-
Section 19: Combine Framework
-
Section 20: SwiftUI Fundamentals for Swift Developers
-
Section 21: Server-Side Swift with Vapor
-
Section 22: Swift Package Manager Deep Dive
-
Section 23: Swift Concurrency Deep Dive
-
Section 24: More Language Features
-
Section 25: Error Handling Deep Dive
-
Section 26: Testing Deep Dive
-
Section 27: Data Structures and Algorithms in Swift
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Swift Macros (Swift 5.9+)
-
Section 31: Property Wrappers Ecosystem
-
Section 32: Swift Interop Deep Dive
-
Section 33: iOS App Architecture Patterns
-
Section 34: Performance and Debugging
-
Section 35: App Distribution and CI/CD
-
Section 36: More Practical Projects
-
Section 37: SwiftData and Persistence
-
Section 38: More Design Patterns
-
Section 39: More Review and Practice
-
Section 40: More Foundation Deep Dive
-
Section 41: Advanced Collections in Swift
-
Section 42: Advanced Generics Practice
-
Section 43: UIKit for Legacy and Hybrid Apps
-
Section 44: watchOS and visionOS Development Basics
-
Section 45: More Networking Patterns
-
Section 46: More Testing Practice
-
Section 47: Accessibility in Swift Apps
-
Section 48: Localization
-
Section 49: More Practical Projects Round 2
-
Section 50: Swift Charts Framework
-
Section 51: More Interview and Algorithm Practice
-
Section 52: Final Practice and Mastery
-
Section 53: Swift Compiler and Build System
-
Section 54: More Concurrency Practice
-
Section 55: App Store Guidelines and Review
-
Section 56: More Design and Architecture
125: Big-O Notation with Swift Examples
Why can't I just use a stopwatch to see which function is faster?
I get this question a lot. It seems intuitive: just wrap your code in a timer, run it, and see which one finishes first. The problem is that timers measure wall-clock time, which is noisy. Your results will change based on whether your MacBook is plugged in, how many Chrome tabs you have open, or if the OS decided to run a background update mid-test.
Big-O notation isn't about seconds; it's about scaling. It describes how the runtime of your function grows as the input size (which we call n) grows. If you have a list of 10 users, a slow algorithm is invisible. If you have 10 million users, the difference between an O(n) algorithm and an O(n²) algorithm is the difference between a fraction of a second and your app hanging for three hours.
How do I actually "calculate" Big-O when looking at my Swift loops?
You don't need a PhD in math to do this. For most of the Swift code you'll write, you just need to count how many times you're iterating over your data. If you have one loop going through an array, that's typically O(n)—linear time.
The "danger zone" is usually nested loops. If you loop through an array, and inside that loop, you loop through the same array again, you've just hit O(n²)—quadratic time. I've seen plenty of juniors accidentally write O(n²) code when trying to find duplicates in a list. Take a look at this:
func hasDuplicates(items: [Int]) -> Bool {
for i in 0..
If you double the number of items in that array, the work doesn't double—it quadruples. That's why we try to avoid nested loops over the same collection whenever possible.
What's the real-world difference between O(1) and O(n) in Swift collections?
This is where understanding your data structures pays off. In Swift, accessing an element in an Array by its index is O(1). It doesn't matter if the array has ten elements or ten billion; the computer knows exactly where that memory address is and jumps straight to it. That's "constant time."
However, searching for a specific value in an unsorted array is O(n). Swift has to check every single element from the start until it finds a match. Here is the contrast:
let numbers = [10, 20, 30, 40, 50]
// O(1) - Constant time. Instant access.
let secondItem = numbers[1]
// O(n) - Linear time. We might have to check every item.
let index = numbers.firstIndex(of: 50)
Pro tip: This is why we use Dictionaries. Checking if a key exists in a Dictionary is O(1) on average. If you find yourself calling firstIndex(of:) inside a loop, you're accidentally creating an O(n²) nightmare. Switch to a Set or a Dictionary instead.
Is O(log n) just academic, or will I actually use it?
It's very real. The classic example is a Binary Search. Imagine you have a sorted list of a million usernames and you're looking for one. A linear search (O(n)) might take a million checks. A binary search (O(log n)) will find it in about 20 checks. It does this by splitting the search area in half with every single step.
You'll see this logic everywhere in high-performance systems. While Swift's standard library handles a lot of this under the hood, knowing that sorting your data first allows you to move from O(n) to O(log n) search time is a superpower when you're optimizing a sluggish app.
📋 Practical Task
Refactoring the Duplicate User Finder
You have been handed a piece of code that checks if a list of User IDs contains any duplicates. Currently, it uses a nested loop, making it O(n²), and it's causing the app to freeze when the user list grows large.
Your Task: Refactor the hasDuplicates function to use a Set. By leveraging the O(1) lookup time of a Set, you should transform the algorithm from O(n²) to O(n) linear time.
struct User {
let id: String
}
func hasDuplicates(users: [User]) -> Bool {
// CURRENT O(n^2) IMPLEMENTATION
for i in 0..
Implement the optimized version and explain in a comment why the Set approach changes the Big-O complexity.
There are no comments for now.