Big O Notation

HackerRank · Beginner ·⚡ Algorithms & Data Structures ·9y ago
Learn about Big O notation, an equation that describes how the run time scales with respect to some input variables. This video is a part of HackerRank's Cracking The Coding Interview Tutorial with Gayle Laakmann McDowell. http://www.hackerrank.com/domains/tutorials/cracking-the-coding-interview?utm_source=video&utm_medium=youtube&utm_campaign=ctci

What You'll Learn

The video covers Big O notation, an equation that describes how the runtime scales with respect to some input variables, and provides four important rules to know when working with Big O notation: adding steps, dropping constants, using different variables for different inputs, and dropping non-dominant terms. The video uses examples in code and real-world scenarios to illustrate these concepts.

Full Transcript

Hi, I'm Gail Lock Mcdow, author of Cracking Coding Interview. Today, we're going to cover one of my favorite topics, which is bigo or algorithmic efficiency. I'm going to start off with a story. Back in 2009, there was this company in South Africa and they had really slow internet they were really, really frustrated by. And they actually had two offices about 50 miles away. So, they set up this little test to see if it would be faster to transfer a big chunk of data over the internet with their really slow internet service provider or via carrier pigeon. Literally, they took a bunch of USB sticks or probably actually one giant one, strapped it to a pigeon and flew it from one office to the next. And they got a ton of press over this test and uh media kind of loves this stuff, right? And of course, the pigeon won, right? it wouldn't be a funny story otherwise. And so they got a whole bunch of press over it and people were like, "Oh my god, I can't believe that a pigeon beat the internet." But the problem was it was really actually kind of a ridiculous test. Cuz here's the thing, transferring data over the internet takes longer and longer and longer with how much data there is. With certain simplifications and assumptions, that's not really the case with pigeons. Pigeons might be really slow or fast, but it always basically takes the same amount of time for a pigeon to transfer one transfer some chunk of data from one office to the next. It just has to fly 50 miles. It's not going to take longer because that USB stick contains more data. So, of course, for a sufficiently large file, the pigeon's going to win. So in big O, the way we talk about this is we describe the pigeon transfer speed as O of one. It's constant time with respect to the size of the input. It doesn't take longer with more input. Again, with certain simplifications and assumptions about the pigeon's ability to carry a USB stick, but the internet time is internet transfer time is O of N. It scales linearly with respect to the amount of input. twice amount of data is going to take roughly twice as much time. So that's what bigo is. Bigo O is essentially an equation that describes how the runtime scales with respect to some input variables. So I'm going to talk about four specific rules, but first let me show you what this might look like in code. So let's consider this simple function that just walks through an array and checks if it contains a particular value. So you would describe this as of n where n is the size of the array. What about this method that walks through an array and prints all pairs of values in the array. Note that this will print if it has the elements five and two in the array, it'll print both 2a 5 and 5a 2. So you would describe this probably as o of n squared where n is the size of the array. You can see that because it has n elements in the array. So it has n squared pairs. So the amount of time it's going to take you to run that function is going to increase with respect to n^2. Okay. So that's kind of the basics of big O. Let's take another real world example. How would you describe the runtime to mow a square plot of land? So a square plot of grass. So you have this plot of grass and you need to mo mow all of it. What's the runtime of this? Well, it's kind of an interesting question, but you could describe it one of two ways. One of which, well, one way you could describe it is O of A, where A is the amount of area in that plot of land. Remember that this is just an equation that expresses how the time increases. So, you don't have to use N in your equation. You can use other variables and often it makes sense to do that. So you can just describe this as O of A where A is the the amount of area there. You could also describe this as O of S^ squ where S is the amount of is this length of one side because it is a square plot of land. So the amount of area is S^ squ. So it's important that you realize that this O of A and this O of S squ are obviously saying essentially the same thing. Just like when you describe the area of a square. Well, you could describe it as 25 or you could describe it as five squared. Just because it has a square and it doesn't make it bigger. So, both are valid ways of describing the runtime. It just depends on what might be clear for that problem. So, with that said, let me go on to four important rules to know with big O. The first one is if you have two different steps in your algorithm, you add up those steps. So if you have a first step that takes, you know, O of A time and a second step that takes O of B, you would add up those run times and you get O of A plus B. So for example, you have this runtime that you you have this algorithm that first um walks through one array and then it walks through this other way. It's going to be the amount of time it takes to walk through each of them. So So you'll add up their run times. The second way, the second rule is that you drop constants. So let's look at these two different ways that you can print out the min and max element in an array. One finds the min element and then finds the max element. The other one finds the min simultaneously. Now these are fundamentally doing the same thing. They're doing essentially the exact same operations just doing them in slightly different orders. Both of these get described as O of N where N is the size of the array. Now, it's really tempting for people to sometimes see two different loops and describe this as O of 2N. And so, it's important that you remember that you drop constants in big O. You do not describe things O of 2N or O of 3 N because you're just looking for how things scale roughly. Is it a linear relationship? Is it a quadratic relationship? So, you always drop constants. The third rule is that if you have different inputs, you're usually going to use different variables to represent them. So let's take this example where we have two arrays and we're walking through them to figure out the number of elements in common between the two arrays. Some people will mistakenly call this as O call this O of N squ, but that's not correct. When you just talk about runtime, if you describe things as O of N or O of N squ or O of N log N, N must have a meaning. It's not like things are inherently that one or the other. So when you describe this O squ, it really doesn't make sense because it doesn't you what what does N mean? N is not the size of the array if there's two different arrays. What you want to describe instead is O of A * B. Again, fundamentally big O is an equation that expresses how the runtime changes, how it scales. So you describe this O of A time B. The fourth rule is that you drop non-dominant terms. So let's suppose you have this code here that walks through an array and prints out the biggest element and then for some reason it goes and prints all pairs of values in the array. So that first step takes O of n time. The second step takes o of n squ time. So um you could first get as a you know less simplified form you could describe this as O of n + n^2 but note that you know compare this to of n and the runtime or compare this to the runtime of n^2 and the runtime of n^2 + n^2 both of those two run times reduce down to n^2 and what's interesting here is that of n plus n^2 should logically be between them. So n this n squ thing kind of dominates this O of N thing. And so you drop that non-dominant term. It's the n square that's really going to drive how the runtime changes. And if you want, you can look up a more academic explanation for when exactly you drop things and when exactly you can't. But this is kind of the layman's explanation for it. We've now covered the very basic pieces of bigo. This is a incredibly important concept to master for your interviews. So, don't be lazy here. Make sure you really, really understand this and try a whole bunch of exercises to solidify your understanding. Good luck.
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from HackerRank · HackerRank · 17 of 60

1 HackerRank for Professors
HackerRank for Professors
HackerRank
2 Why CareerCup's Gayle Laakmann McDowell Loves HackerRank [Testimonial]
Why CareerCup's Gayle Laakmann McDowell Loves HackerRank [Testimonial]
HackerRank
3 How to Leverage Data-Driven Insights to Improve Recruiting Results
How to Leverage Data-Driven Insights to Improve Recruiting Results
HackerRank
4 Hire Top Linux Talent and System Admins with SudoRank [Webinar]
Hire Top Linux Talent and System Admins with SudoRank [Webinar]
HackerRank
5 How Zenefits Hired 10 Engineers in 10 Days with HackerRank's CodeSprint
How Zenefits Hired 10 Engineers in 10 Days with HackerRank's CodeSprint
HackerRank
6 Hire the hottest SQL talent with DbRank from HackerRank [Tutorial]
Hire the hottest SQL talent with DbRank from HackerRank [Tutorial]
HackerRank
7 How to Use HackerRank's CodePair Tool [Tutorial]
How to Use HackerRank's CodePair Tool [Tutorial]
HackerRank
8 Find Linux Talent with SudoRank challenges [Tutorial]
Find Linux Talent with SudoRank challenges [Tutorial]
HackerRank
9 VMware's Mat Connot details his favorite new way to engage top programmers
VMware's Mat Connot details his favorite new way to engage top programmers
HackerRank
10 An Inside Look at Coinbase's Recruiting Strategy [webinar]
An Inside Look at Coinbase's Recruiting Strategy [webinar]
HackerRank
11 How Evernote Enhanced Their Tech Recruiting Process
How Evernote Enhanced Their Tech Recruiting Process
HackerRank
12 5 strategies for being a tech recruiting champion with HackerRank [Webinar]
5 strategies for being a tech recruiting champion with HackerRank [Webinar]
HackerRank
13 HackerRank Story - Lets make the world flat!
HackerRank Story - Lets make the world flat!
HackerRank
14 Data Structures: Hash Tables
Data Structures: Hash Tables
HackerRank
15 Data Structures: Anagram Problem Solution
Data Structures: Anagram Problem Solution
HackerRank
16 Algorithms: Solve 'Lonley Integer' Using Bit Manipulation
Algorithms: Solve 'Lonley Integer' Using Bit Manipulation
HackerRank
Big O Notation
Big O Notation
HackerRank
18 Algorithms: Graph Search, DFS and BFS
Algorithms: Graph Search, DFS and BFS
HackerRank
19 Algorithms: Bit Manipulation
Algorithms: Bit Manipulation
HackerRank
20 Data Structures: Solve 'Find the Running Median' Using Heaps
Data Structures: Solve 'Find the Running Median' Using Heaps
HackerRank
21 Data Structures: Heaps
Data Structures: Heaps
HackerRank
22 Algorithms: Memoization and Dynamic Programming
Algorithms: Memoization and Dynamic Programming
HackerRank
23 Algorithms: Sort An Array with Comparator
Algorithms: Sort An Array with Comparator
HackerRank
24 Algorithms: Solve 'Coin Change' Using Memoization and DP
Algorithms: Solve 'Coin Change' Using Memoization and DP
HackerRank
25 Data Structures: Solve 'Contacts' Using Tries
Data Structures: Solve 'Contacts' Using Tries
HackerRank
26 Data Structures: Tries
Data Structures: Tries
HackerRank
27 Algorithms: Bubble Sort
Algorithms: Bubble Sort
HackerRank
28 Algorithms: Merge Sort
Algorithms: Merge Sort
HackerRank
29 Algorithms: Quicksort
Algorithms: Quicksort
HackerRank
30 Data Structures: Binary Search Tree
Data Structures: Binary Search Tree
HackerRank
31 Data Structures: Trees
Data Structures: Trees
HackerRank
32 Algorithms: Solve 'Shortest Reach' Using BFS
Algorithms: Solve 'Shortest Reach' Using BFS
HackerRank
33 Algorithms: Solve 'Connected Cells' Using DFS
Algorithms: Solve 'Connected Cells' Using DFS
HackerRank
34 Algorithms: Solve 'Recursive Staircase' Using Recursion
Algorithms: Solve 'Recursive Staircase' Using Recursion
HackerRank
35 Algorithms: Binary Search
Algorithms: Binary Search
HackerRank
36 Algorithms: Solve 'Ice Cream Parlor' Using Binary Search
Algorithms: Solve 'Ice Cream Parlor' Using Binary Search
HackerRank
37 Data Structures: Linked Lists
Data Structures: Linked Lists
HackerRank
38 Data Structures: Cycles in a Linked List
Data Structures: Cycles in a Linked List
HackerRank
39 Data Structures: Stacks and Queues
Data Structures: Stacks and Queues
HackerRank
40 Data Structures: Queue With Two Stacks
Data Structures: Queue With Two Stacks
HackerRank
41 Data Structures: Balanced Parentheses in Expression
Data Structures: Balanced Parentheses in Expression
HackerRank
42 Algorithms: Recursion
Algorithms: Recursion
HackerRank
43 How to Hire for Your Startup [Collision Panel Video]
How to Hire for Your Startup [Collision Panel Video]
HackerRank
44 HackerRank main() San Francisco | November 8, 2018
HackerRank main() San Francisco | November 8, 2018
HackerRank
45 The Enterprise Playbook for Technical Hiring
The Enterprise Playbook for Technical Hiring
HackerRank
46 Remote Hiring Colloquy with EY's Dipika Kapadia & BigBasket's Rupesh Kumar
Remote Hiring Colloquy with EY's Dipika Kapadia & BigBasket's Rupesh Kumar
HackerRank
47 Remote Hiring Colloquy with Hackerrank's Hari K, Akamai's Neha Akhouri & Infosys's Rafee Tarafdar
Remote Hiring Colloquy with Hackerrank's Hari K, Akamai's Neha Akhouri & Infosys's Rafee Tarafdar
HackerRank
48 Building a Culture for Developer Innovation
Building a Culture for Developer Innovation
HackerRank
49 Developer Trends in 2020 and Beyond
Developer Trends in 2020 and Beyond
HackerRank
50 HackerRank's Livestream: Accelerating Developer Innovation at PhonePe
HackerRank's Livestream: Accelerating Developer Innovation at PhonePe
HackerRank
51 Aadil Bandukwala converses with Rahul Chari about HackerRank's upcoming livestream
Aadil Bandukwala converses with Rahul Chari about HackerRank's upcoming livestream
HackerRank
52 Accelerating Developer Innovation at PhonePe with Rahul Chari
Accelerating Developer Innovation at PhonePe with Rahul Chari
HackerRank
53 The Future of Work for Developers
The Future of Work for Developers
HackerRank
54 Scaling AI from Proof of Concept to Production
Scaling AI from Proof of Concept to Production
HackerRank
55 The Secret Sauce to getting hired in Myntra’s Tech Team
The Secret Sauce to getting hired in Myntra’s Tech Team
HackerRank
56 Reimagining Money with Paypal
Reimagining Money with Paypal
HackerRank
57 Engineering Predictions for 2021 and Beyond! 🎬
Engineering Predictions for 2021 and Beyond! 🎬
HackerRank
58 Future-forward Tech & Careers with BNY Mellon
Future-forward Tech & Careers with BNY Mellon
HackerRank
59 How Atlassian Approaches Diversity and Inclusion with Balance & Belonging
How Atlassian Approaches Diversity and Inclusion with Balance & Belonging
HackerRank
60 Connecting Global Tech Ecosystems: Andela Shines a Light on Africa’s Developer Talent
Connecting Global Tech Ecosystems: Andela Shines a Light on Africa’s Developer Talent
HackerRank

The video teaches Big O notation and its importance in understanding the runtime complexity of algorithms. It provides four key rules to work with Big O notation and uses examples to illustrate these concepts. By mastering Big O notation, viewers can improve their skills in analyzing and optimizing algorithms.

Key Takeaways
  1. Understand the concept of Big O notation and its importance in algorithmic complexity
  2. Learn the four key rules: adding steps, dropping constants, using different variables for different inputs, and dropping non-dominant terms
  3. Practice applying these rules to examples in code and real-world scenarios
  4. Analyze the runtime complexity of algorithms using Big O notation
  5. Optimize algorithms for better performance using Big O notation
💡 Big O notation is a crucial concept in understanding the runtime complexity of algorithms, and mastering it can help improve skills in analyzing and optimizing algorithms.

Related AI Lessons

Bloom Filters, Explained Properly
Learn how Bloom filters work and their benefits, including tiny memory and blazing speed, in exchange for potential false positives.
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Learn how prefix sums enable instant range queries in arrays, boosting performance in various applications
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
A simple math question can destroy a developer's interview, highlighting the importance of being prepared for unexpected questions
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Learn to remove duplicates from a sorted array using the two pointers technique, improving from brute force to optimized solutions
Medium · Python
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →