Check out a free preview of the full Complete Intro to Computer Science course
The "Why Use Big O" Lesson is part of the full, Complete Intro to Computer Science course featured in this preview video. Here's what you'd learn in this lesson:
Brian discusses how to easier determine the appropriate Big O for a process and demonstrates some practical applications of Big O including a comment system with a filtering ability. The key to determining the appropriate amount of Big O is getting as much context of the algorithm as possible.
Transcript from the "Why Use Big O" Lesson
[00:00:00]
>> So, why is this useful to one? Again, this is gonna be really useful when you go interview at tech companies because they love to ask you, what's the Big O of the algorithm you just wrote? And I kinda gave you some tricks, which is like look for four loops, but also you need to have a bit more critical eye in terms of just think about in general, the more items I add to this array, how much longer is it gonna take, right?
[00:00:25]
The more inputs I give it does it take longer? So, that four loop trick doesn't always work in the sense that there can be other things that can contribute to it, but just be aware of that. Let's talk about why you would use this actually in a day job.
[00:00:41]
So I gave an example here, which is like let's say you're writing a comment system for a website, and that comment system had a sorting and filtering ability. What's the appropriate Big O for assorting and filtering algorithm for a comment system? Now, the response I want you to have to that question is, you didn't give me enough information, it depends, right.
[00:01:11]
That's why whenever you ask a computer scientist a question, their answer is like 99 times out of 100, it depends, I need more information. So, I know it annoys people to say it depends, give me more information, but it's actually the correct answer to that question. Because, if you're sorting and filtering for comments on a, like a small colleges or site any given article there's only three or four different comments, the answer is it doesn't matter, [LAUGH] right.
[00:01:44]
It doesn't matter if you have complexity of Big O of n cubed. Because if you're only sorting and filtering three or four comments at a time, it really just profoundly doesn't matter how fast that algorithm takes, because the difference between three milliseconds and 15 milliseconds is basically negligible to an end user, mostly.
[00:02:09]
But if you can write better code, more understandable, more maintainable code, so that people can come back and maintain your code later and better, that's what you wanna do, right. That's the correct answer of that question. It's not write the fastest sorting and filtering algorithm because it just doesn't make any difference.
[00:02:26]
My point here is, don't spend time on things that don't matter, right. But I used to work at Reddit, right, and if Reddit had a sort and filtering algorithm of n cubed, you would never see reddit.com ever again because it gets so many comments, right. So the Big O of that sorting algorithm makes a massive difference.
[00:02:55]
It's something that was unscrapable probably crashed the site consistently. So this is why Big O is necessary in the sense of like, it's a tool, right. I didn't give you the way to judge this algorithm is better than this algorithm, that was not my intention, right. That's like saying that like if I gave you a 12 inch ruler, you would never need a tape measure, right.
[00:03:17]
It's just one tool in your toolbox for measuring algorithms, but you need to compare things across multiple axes, right. This is where I'm getting back to, this is the science of trade offs, right. And I'm giving you one of the measurement sticks here to measure your algorithms. Yeah, the question is always got to be, it depends give me more information, what do you need me to do?
[00:03:43]
And like again, this is kind of a pro tip for interviewing as well. Pull as much information out of the interviewer that you possibly can, right. If they just say, Hey, we're writing a sorting for these comments, don't just pop in there and start writing a sorting algorithm right away, that's the incorrect answer.
[00:03:59]
You wanna say, what am I doing? Who's the end user? What's my constraints? What devices am I running on? Am I running on 5G? Am I running on 2G, right? These are the kinda questions that you need to be thinking about. You need to paint a complete picture of what the actual requirements are.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops