Skip to content

Big O notation

big-O-cheat-sheet.jpeg

๐…๐ข๐ซ๐ฌ๐ญ๐ฅ๐ฒ, ๐ฐ๐ก๐š๐ญ ๐ข๐ฌ ๐๐ข๐  ๐Ž ๐๐จ๐ญ๐š๐ญ๐ข๐จ๐ง?

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.