Big O notation
๐ ๐ข๐ซ๐ฌ๐ญ๐ฅ๐ฒ, ๐ฐ๐ก๐๐ญ ๐ข๐ฌ ๐๐ข๐ ๐ ๐๐จ๐ญ๐๐ญ๐ข๐จ๐ง?
Big O describes an algorithm's runtime or memory consumption without the interference of contextual variables like RAM and CPU.
It gives programmers a way to compare algorithms and identify the most efficient solution.
โโโ
๐๐ข๐ ๐ ๐๐ง๐ฌ๐ฐ๐๐ซ๐ฌ ๐จ๐ง๐ ๐ฌ๐ญ๐ซ๐๐ข๐ ๐ก๐ญ๐๐จ๐ซ๐ฐ๐๐ซ๐ ๐ช๐ฎ๐๐ฌ๐ญ๐ข๐จ๐ง:
"How much does runtime or memory consumption grow as the size of the input increases, in the worst-case scenario?".
โโโ
๐๐จ ๐ฎ๐ญ๐ข๐ฅ๐ข๐ณ๐ ๐๐ข๐ ๐, you'll need to know the possible values and how they compare with each other.
Use the attached photo for a quick reference.
๐๐จ, ๐ก๐จ๐ฐ ๐๐จ ๐ฒ๐จ๐ฎ ๐๐ฉ๐ฉ๐ฅ๐ฒ ๐๐ข๐ ๐ ๐ข๐ง ๐ฒ๐จ๐ฎ๐ซ ๐ญ๐๐๐ก๐ง๐ข๐๐๐ฅ ๐ข๐ง๐ญ๐๐ซ๐ฏ๐ข๐๐ฐ๐ฌ?
Here are a few scenarios where Big O can be used:
๐ธ Live coding challenges ๐ธ Code walk-throughs ๐ธ Discussions about projects/solutions you've built ๐ธ Discussions about your approach to programming & problem-solving
When any of these scenarios come up, be sure to mention the Big O of your solution and how it compares to alternative approaches.
This is especially useful in live coding challenges where you have to compare solutions on the spot โ remember to think out loud!
โโโ
๐๐ข๐ฉ: ๐๐ก๐๐ง ๐๐จ๐ฆ๐ฉ๐๐ซ๐ข๐ง๐ ๐ฌ๐จ๐ฅ๐ฎ๐ญ๐ข๐จ๐ง๐ฌ, ๐ฉ๐๐ฒ ๐๐ญ๐ญ๐๐ง๐ญ๐ข๐จ๐ง ๐ญ๐จ ๐ญ๐ก๐ ๐ฉ๐ซ๐จ๐๐ฅ๐๐ฆโ๐ฌ ๐ซ๐๐ช๐ฎ๐ข๐ซ๐๐ฆ๐๐ง๐ญ๐ฌ.
For example, linear complexity may be completely fine when the input can never be too large.
But if youโre dealing with big data, youโll want to opt for something more efficient.
Of course, the goal is to get the correct Big O notation that applies to your solution.
But don't worry about getting it wrong!
Your interviewer will probably correct you when you do.
The point is to show that you are thinking about the efficiency and performance of your solution.
๐๐จ ๐ญ๐ก๐ข๐ฌ ๐๐ง๐ ๐ฒ๐จ๐ฎ'๐ฅ๐ฅ ๐๐ ๐๐๐ฅ๐ ๐ญ๐จ ๐ฌ๐ก๐จ๐ฐ๐๐๐ฌ๐ ๐๐ง ๐ข๐ฆ๐ฉ๐จ๐ซ๐ญ๐๐ง๐ญ ๐ญ๐ซ๐๐ข๐ญ ๐ญ๐ก๐๐ญ ๐ญ๐๐๐ก๐ง๐ข๐๐๐ฅ ๐ก๐ข๐ซ๐ข๐ง๐ ๐ฆ๐๐ง๐๐ ๐๐ซ๐ฌ ๐ฅ๐จ๐จ๐ค ๐๐จ๐ซ:
The ability to consider a solution's viability beyond whether it works or not.
This shows maturity in your decision-making and approach to programming.