How to Never Ever Have a Code Limit
Have you ever noticed that some people can look at a problem and instantly know which algorithm to use? It almost feels like magicâbut itâs not! Today, weâll talk about how you can do it too.
The secret? Pay attention to constraints.
Algorithms arenât like superheroes or villainsâtheyâre just tools. Whether they save the day or cause disaster depends entirely on the constrains. Nobody would be mad at you if you sort 10-items array even with exponential sort. Once you know how to use constrains to your advantage, you will know exactly where you can slack, and there you should try your harderst.
One Number to Remember: 10âš
There is a good rule of thumb for competitive programming:
computers can handle ~ 10âš
basic operations per second.
Modern CPUs run at GHz speeds, meaning they can process billions of instructions per second.
The prefix "Giga" literally means 10âš
.
Keep in mind, that real-world performance varies depending on factors like cache efficiency, memory access, and CPU architecture.
But number 10âš
is good enough for back-of-envelope estimations.
This is roughly the number of basic operations your process can handle per second.
Estimating Running Time Using Constraints
Letâs apply this idea to some coding problems.
Example 1: Two Sum
Problem Statement
Given an array nums
and an integer target
, return the indices of two numbers that add up to target
.
Constraints:
2 ⤠nums.length ⤠10â´
Before even thinking about an algorithm, check the constraints. The input size is at most 10â´
, and most coding platforms allow 1 second to run the solution.
A quadratic solution (O(n²)
) would require ~(10â´)² = 10â¸
operations, which is less than 10âš
, meaning it should pass just fine.
Thatâs exactly why this problem is easyâeven a simple brute-force approach is fast enough!
Example 2: Longest Substring Without Repeating Characters
Problem Statement
Given a string s
, find the length of the longest substring without duplicate characters.
Constraints:
0 ⤠s.length ⤠5 Ă 10â´
If we try a quadratic algorithm, weâd have to do (5 Ă 10â´)² = 25 Ă 10⸠= 2.5 Ă 10âš
operations. Since our computer can handle 10âš
operations per second, this would take 2.5 secondsâtoo slow!
That means a quadratic approach is too slow for this problem. We need something better, like an O(n log n)
or O(n)
solution:
5 Ă 10â´ Ă log(5 Ă 10â´) â 8 Ă 10âľ
Since 8 Ă 10âľ
is way less than 10âš
, an optimized algorithm will run in time. This is why the problem is mediumâbrute force wonât cut it.
How to Use This in a Coding Interview
Coding Assessments
This technique is super useful for automated coding assessments, where you need to impress the system, not a human. If your code times out, knowing how to estimate complexity will help you quickly adjust your approach.
Live Interviews: Always Ask About Constraints
Some interviewers intentionally leave constraints vague to see if you ask for them.
Be sure to enquire:
- How large is the input?
- How often will this function be called?
This helps you understand what matters mostâshould you optimize for time, space, or simplicity? With this mindset, youâll make smarter design choices and impress interviewers.
To help you remember, hereâs a fun video for you:
TL;DR
- Remember
10âš
âthe number of operations per second your computer can handle. - Check constraints firstâthey tell you what kind of algorithm will work.
- Estimate running timeâuse quick calculations to see if your approach will be too slow.
- Use this in interviewsâask about constraints to pick the best solution!
Master this, and youâll never have to worry about time limits again!