After graduating from my Software Engineering program at Flatiron school, I’ve been adjusting to the world of self teaching. One thing however, became evidently clear within the couple months leading up to graduation. Through the program and side courses I’ve taken, I know how to write code. But what separates good code from great code? Throughout my bootcamp we were taught to maintain the DRY principle (Don’t Repeat Yourself). This is a strong foundation for writing good code but as I quickly learned, that’s not all there is to it. While reading through “Cracking the Coding Interview”, a book I highly recommend, I was introduced to the concept of Big O Notation.
While diving into the topic, I came to the realization that this is an extremely important concept when it comes to programming. Big O Notation measures the quality of code by analyzing the time and space complexity of a given algorithm. As represented on the graph below, the more horizontal the line is the better the complexity is. For the purpose of this article, I’ll be specifically covering Big O Notation as it refers to time, not space.
Simply put, Big O Notation when referring to time complexity, describes the runtime of a given algorithm. As applications gain more traction, scalability becomes a huge factor. This is where Big O Notation plays an integral part. As data size increases, some algorithms can take longer to execute as now there are more pieces of the puzzle to evaluate. Therefore the more efficient we can make our code execute, the better.
As shown in the graph above, the primary Big O runtimes from fastest to slowest are: Constant O(1), Logarithmic O(log N), Linear O(n), Log Linear O(n log(n), Quadratic O(n²), Exponential O(2^n), and lastly Factorial O(n!). Rather than breaking down each runtime by definition which can be pretty math focused, I’ve learned a few tips to help me identify which I’m working with. Below I put together a list of basic definitions and hints for for each runtime.
After gaining a good sense of time complexity with Big O Notation, I’ve moved on to space complexity. Space complexity is caused by variables, data structures, function calls, and other memory allocations. I plan to further dive into this topic, as well as continue improving my use of time complexity to become a better programmer. Although I finished bootcamp, an exciting journey still awaits.