Is Python too slow for coding interviews?
Is Python too slow for coding interviews? Almost never, but there are three specific situations where it bites. Here are the real numbers, the operation budgets, and the fixes that matter.
Is Python too slow for coding interviews? Almost never. Interviewers judge your complexity class, not your constant factor, and the inputs in a live session are tiny. Python only hurts on online judges with large inputs, tight nested loops, or deep recursion, and each of those has a known fix.
I have written solutions in Python, Java and C++ for years, and I still reach for Python first when I am under a clock. That is a position, not a universal rule, so this essay lays out the evidence: how much slower Python really is, what that means in operations per second, where it actually costs you a test case, and what to do about it.
How much slower is Python than C++ or Java?
For code that spends its time in hand-written loops, CPython is roughly 30 to 100 times slower than C++. For code that leans on built-in functions, the gap often shrinks to a factor of two or three, and sometimes disappears.
The loop figure is easy to check yourself. A loop of ten million iterations that adds a number to a running total takes around 0.4 to 0.8 seconds in CPython on a typical laptop, depending on version. The same loop in optimised C++ finishes in a handful of milliseconds, because the compiler may even collapse it to a constant. Java sits in between, usually 10 to 30 milliseconds after warm-up.
Here is the framing I use, and I have not seen it written down elsewhere: the toll booth model. Every bytecode instruction the Python interpreter executes pays a toll of roughly 30 to 60 nanoseconds. A line such as total += a[i] pays several tolls: load the list, load the index, subscript, add, store. A call to sum(a) pays one toll to enter, then the C code inside runs at native speed across the whole list. The difference between slow Python and fast Python is mostly the number of times you drive through the booth. Hand-written loops drive through once per element. Built-ins drive through once per batch.
That one idea predicts nearly everything else in this essay. It explains why sorted() on a million integers takes about a third of a second while a Python-written merge sort on the same data takes ten times longer. It explains why a list comprehension beats an append loop. And it explains why the language rarely matters when you pick the right algorithm: choosing the right algorithm removes millions of toll booths at once.
How many operations can Python handle in one second?
Plan for about 10 million simple operations per second in pure Python, against 100 million to 1 billion in C++. Treat 107 as a ceiling and 3 to 5 million as the number you actually trust when the loop body does anything non-trivial.
Turn that into input sizes, assuming a one to two second limit and hand-written loops:
- O(n): comfortable up to about 106. A single pass with a dictionary lookup per element costs maybe 0.15 to 0.3 seconds.
- O(n log n): comfortable up to about 2 x 105 if you write the log factor yourself (a heap push per element, say). If the log factor hides inside
sorted()orheapq, you can go several times higher. - O(n2): comfortable up to about 3,000 to 5,000. Beyond that you are in trouble.
- O(2n): up to about n = 20, since 220 is a little over a million and each subset usually costs more than one operation.
Compare that with C++, where O(n2) is fine to roughly 104 and O(n log n) reaches 106 or more. Python shifts every line left by about one notch. It does not change which complexity classes are viable.
What does the slowdown look like on a real problem?
It looks like a quadratic solution that passes at n = 1,000, crawls at n = 10,000, and dies at n = 100,000, in any language. Take a task I will restate in my own words: given a list of integers and a target, count how many pairs of positions add up to the target. It is a cousin of Two Sum.
def count_pairs_slow(a, t):
c = 0
for i in range(len(a)):
for j in range(i + 1, len(a)):
if a[i] + a[j] == t:
c += 1
return c
def count_pairs_fast(a, t):
seen = {}
c = 0
for x in a:
c += seen.get(t - x, 0)
seen[x] = seen.get(x, 0) + 1
return cNow count the work. With n = 10,000 the slow version runs n(n-1)/2 = 49,995,000 inner iterations. Each one pays for two subscripts, an addition, a comparison and a loop step, call it 60 to 80 nanoseconds. That is roughly 3 to 4 seconds. The same loop in C++ runs in about 50 to 100 milliseconds, which is fine. So here Python fails and C++ passes, and this is exactly the case people point to when they say Python is too slow.
The fast version does 10,000 iterations with two dictionary operations each. That is about 20,000 operations, around 3 to 5 milliseconds. It is roughly a thousand times faster than the slow Python version and comfortably faster than the slow C++ version too.
Push n to 100,000. The slow version needs about 5 x 109 iterations: 6 or more minutes in Python, 3 to 5 seconds in C++. Both fail a one-second limit. The fast version needs around 40 milliseconds. A 50x constant-factor penalty is worth only a square root in input size for a quadratic algorithm, since sqrt(50) is about 7. Python at O(n) beats C++ at O(n2) as soon as n passes a few hundred. That is the arithmetic behind the claim that complexity class dominates language choice.
Do interviewers care about Python's speed?
No, not in the way candidates fear. In a live interview the interviewer wants to know whether your solution is O(n) or O(n2), and whether you can justify it. They will not run it on ten million elements.
I have sat on both sides of that table. The question I have heard most often is some version of "can you do better than this?", and it is always about the exponent or the log factor. I have never heard "your solution is correct and optimal but Python is slow, so I am marking you down." Most companies allow Python, and many interviewers use it themselves. For the reasoning behind how complexity should be stated and defended, see Complexity is a contract, not a fact.
The caveat is the take-home or timed online assessment, where your code is run against hidden tests. Even there the limits are normally set so that the intended algorithm passes in any mainstream language. Some platforms scale the limit for slower languages; others do not. If you hit a time limit with a correct, optimal-complexity solution in Python, treat it as a constant-factor problem and use the techniques below before you consider switching languages.
Where does Python actually lose test cases?
Python loses test cases in three places: tight nested loops over large grids or tables, dynamic programming with millions of states, and recursion deeper than the interpreter allows. Everything else is almost always fine.
Nested loops over big grids. A grid search over 1,000 x 1,000 cells has a million cells, each with four neighbours. That is 4 million neighbour checks, each costing several toll booths. Expect one to three seconds. The algorithm is optimal at O(rows x cols), and the constant still hurts. Number of Islands is fine at LeetCode sizes, but a stress-test variant at that scale is not.
DP with huge state spaces. A table of 5,000 x 5,000 is 25 million cells. At a conservative 100 nanoseconds per cell you spend 2.5 seconds filling it. This is where the choice between top-down and bottom-up matters; see memoisation vs tabulation for why a bottom-up table with rolling rows can be several times faster than a cached recursive function.
Recursion depth. CPython's default recursion limit is 1,000 frames. A DFS on a path-shaped graph with 100,000 nodes will raise a RecursionError long before it runs out of time. You can raise the limit with sys.setrecursionlimit, but very deep recursion can still crash the interpreter on some systems. The robust answer is an explicit stack. I covered the speed side of this in Is recursion slower than iteration?.
How do you make Python solutions faster?
Reduce the number of times you pay the toll: push work into built-ins, pick the right container, and flatten recursion. The table below lists the changes I reach for, with typical effects on a Python 3 interpreter.
| Technique | Typical effect | Trade-off |
|---|---|---|
Replace a hand-written loop with a built-in (sum, min, sorted, any) | 5x to 50x on that loop | Only works when the built-in matches the task |
| List comprehension instead of append loop | 1.3x to 2x | Harder to read when nested |
collections.deque instead of list.pop(0) | O(n) per pop becomes O(1) | No random indexing in the middle |
Bottom-up DP array instead of lru_cache recursion | 2x to 4x, no depth limit | You must work out the iteration order |
| Explicit stack instead of deep recursion | Removes crash risk, small speed gain | More code, less direct to explain |
Read input in one call with sys.stdin | 5x to 10x on input of 106 lines | Only relevant on judges that use standard input |
| Bind hot names to locals | 1.1x to 1.3x | Small gain, clutters code |
Two of these deserve a worked comment. First, list.pop(0): popping from the front of a Python list shifts every remaining element, so a BFS that uses a list as its queue does about n2/2 element moves in the worst case. At n = 100,000 that is 5 x 109 moves, though done in C by memmove, so it takes a couple of seconds rather than minutes. It looks innocent and it fails at scale. A deque gives O(1) at both ends. Second, the built-in rule: the sum of a million integers takes about 5 to 10 milliseconds with sum() and about 60 to 80 milliseconds with a loop. Same algorithm, same complexity, a tenfold difference.
Also lean on the standard library as a set of well-tuned data structures. heapq gives you a heap, bisect gives you binary search on a sorted list, Counter handles frequency counting, and itertools.accumulate builds prefix sums in one call. Each replaces dozens of toll-paying bytecodes with a few.
One more habit that costs nothing: before optimising constants, check the complexity again. Most Python time limit failures I have debugged were not slow constants. They were an accidental O(n) operation inside a loop, such as slicing a list, using in on a list instead of a set, or concatenating strings. The article on why a solution gets time limit exceeded walks through that diagnosis in order.
Should you switch to C++ or Java for interviews?
Switch only if you are equally fluent in the other language. Fluency beats raw speed, because the most common way to fail an interview is not a slow constant but a bug you could not find under pressure.
Python's advantages are concrete. Tuples compare lexicographically, so heap entries like (distance, node) need no comparator. Integers do not overflow, which removes an entire class of bugs in products and sums. Slicing, unpacking, and dictionaries with default behaviour cut a typical solution to half the lines of its Java equivalent. Fewer lines means less to type, less to explain, and fewer places for an off-by-one to hide. When I time myself, a medium problem takes me noticeably longer in C++ purely because of the ceremony, and in a 45-minute slot that matters more than 50x on a loop that will never run on a large input.
The case for switching is narrower: competitive programming with strict limits, or a role where the interview itself is run against large hidden inputs and a language-agnostic limit. In those settings, learn C++ or at least be ready to use PyPy where it is offered. For a standard software engineering loop, I would not trade a language you think in for a language that is faster.
Frequently asked questions
Will Python get me rejected at FAANG-style interviews?
No. Large companies routinely let candidates choose Python, and interviewers evaluate the algorithm, the reasoning and the code quality. The only rejection risk is a solution with the wrong complexity class, which is language-independent.
Is PyPy a fix for slow Python on coding platforms?
Often yes, when the judge offers it. PyPy's just-in-time compiler can run tight numeric loops five to twenty times faster than CPython. It helps less with code that is already built-in heavy, and not every platform supports it.
Does recursion in Python hurt more than in other languages?
Yes, a bit. Python function calls are relatively expensive, around 50 to 100 nanoseconds each, and the default limit of 1,000 frames makes deep DFS unsafe. For anything deeper than a few hundred levels, convert to an explicit stack or a bottom-up loop.
If you want to see these ideas on a problem where the hash map turns an unusable quadratic loop into a few milliseconds, work through Subarray Sum Equals K next and time both versions yourself.