C# Integer Overflow in Coding Interviews: The Complete Guide

intwraps silently in C#'s default unchecked mode, handing you wrong answers without exceptions or warnings- Cast before you multiply:
(long)a * bpromotes the full expression; casting the result after the fact does nothing if overflow already happened - Binary search midpoint: use
left + (right - left) / 2, not(left + right) / 2, when bounds can approachint.MaxValue Math.Abs(int.MinValue)throws anOverflowException; promote tolongfirst to handle this edge case safely- Negative modulo: C#'s
%keeps the dividend's sign, so-7 % 3is-1; use((a % m) + m) % mfor non-negative results BigIntegerviaSystem.Numericshandles exact arithmetic beyondlong's range and is available on most interview platforms- The interview signal: reaching for
longbefore overflow can happen is stronger than scatteringcheckedblocks through your solution
C# feels safe. Strong typing, a helpful compiler, runtime exceptions scattered everywhere like friendly landmines. So when an interviewer asks you to find the median of a large dataset and your answer is -1,073,741,824, you know exactly what happened. Your compiler did not warn you. Your IDE did not complain. C# integer overflow compiles clean, runs without error in most contexts, and delivers a wrong answer with a perfectly straight face.
The Silent Failure You Didn't Ask For
int x = int.MaxValue; // 2,147,483,647 x++; Console.WriteLine(x); // -2,147,483,648
No exception. No warning. The value wraps using two's complement and keeps executing as if everything is fine. Which it is not.

This is the number line your program is walking. The right edge connects directly to the left edge, and C# won't tell you when you fall off.
Going the other direction works the same way:
int y = int.MinValue; // -2,147,483,648 y--; Console.WriteLine(y); // 2,147,483,647
This is the unchecked default, inherited from C and C++ behavior. The compiler trusts that you know what fits in a 32-bit integer. In an interview, that trust becomes your problem.
For a deeper look at why the bit pattern works this way, see Two's Complement Overflow: Negative Numbers and the Bugs They Hide.
The Ranges You Need to Memorize
Most interview problems care about two types:
| Type | Bits | Range |
|---|---|---|
int | 32 | -2,147,483,648 to 2,147,483,647 |
long | 64 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
uint | 32 | 0 to 4,294,967,295 |
ulong | 64 | 0 to 18,446,744,073,709,551,615 |
If your values might reach 10^10 or higher, switch to long. When a problem says "given n up to 10^9, find the sum," and n can be 10^5 elements, your sum can hit 10^14. int will not survive that.
More on this in int32 vs int64: The Overflow Bug Every Coding Interview Tests.
Where This Actually Shows Up in Interviews
Binary Search Midpoint
// Wrong: left + right overflows when both are near int.MaxValue int mid = (left + right) / 2; // Correct int mid = left + (right - left) / 2;
This is the most famous overflow bug in all of programming. Jon Bentley wrote the original binary search in his 1986 book Programming Pearls. It had this exact bug. Joshua Bloch found it in 2006 and wrote a post about it. It was in Java's standard library for nine years. If the bug lived undetected in a textbook and a production standard library, it will absolutely hide in your interview solution.
The fix is algebraically equivalent and overflow-safe. Reach for it by habit.
Products of Large Values
int a = 100_000, b = 100_000; long wrong = a * b; // overflow happens before the assignment long correct = (long)a * b; // cast one operand first
Cast before multiplying, not after. The multiplication happens first, in int territory, and then the (already corrupted) result gets promoted to long. Once one operand is long, the entire expression promotes and you're safe.
Factorials and Combinatorics
int overflows at 13! (6,227,020,800 exceeds int.MaxValue). long overflows at 21!. If a problem asks for an exact factorial beyond 20, you need BigInteger. If it asks for values modulo 10^9+7, use long with modular arithmetic and you're fine.
Math.Abs on int.MinValue
Math.Abs(int.MinValue); // throws OverflowException
The absolute value of -2,147,483,648 is 2,147,483,648. That does not fit in a signed 32-bit integer. C# is one of the few languages that actually throws here rather than silently returning the wrong value, which is considerate of it. Promote before calling:
long safeAbs = Math.Abs((long)int.MinValue); // 2,147,483,648
This bites candidates writing negation logic, palindrome number checks, or anything that takes absolute values of arbitrary inputs. The exception message in the middle of your interview is not a great time to discover this.
Negative Modulo
int a = -7; Console.WriteLine(a % 3); // -1, not 2
C# uses truncated division. The result of % carries the sign of the dividend. When you need a non-negative result, the fix is one line:
int result = ((a % m) + m) % m;
This matters any time your hash or circular-index logic might receive a negative input. Forgetting it means your circular buffer wraps the wrong direction and everything is fine until it isn't.
The checked Keyword: It Exists, You Probably Won't Use It
C# provides checked to convert silent overflow into a thrown OverflowException. It works as a block or an expression:
// Block form checked { int x = int.MaxValue; int y = x + 1; // throws OverflowException } // Expression form int z = checked(int.MaxValue + 1); // throws OverflowException
The inverse, unchecked, explicitly requests the silent-wrapping behavior you already have by default:
unchecked { int wrapped = int.MaxValue + 1; // -2,147,483,648, no exception }
You can enable checked mode project-wide with <CheckForOverflowUnderflow>true</CheckForOverflowUnderflow> in your .csproj. Most interview platforms don't, so the default you're running against is unchecked.
The right move in an interview is to choose the correct type, not scatter checked blocks everywhere. The signal your interviewer wants to see is that you reached for long before the overflow could happen. Wrapping everything in checked reads like you don't know which values are safe and you're hoping the exception tells you.
Convert.ToInt32() Throws. Casting Doesn't.
long big = 3_000_000_000L; int a = (int)big; // -1294967296 (silent truncation) int b = Convert.ToInt32(big); // throws OverflowException int c = checked((int)big); // throws OverflowException
When narrowing from long to int and the value might be out of range, Convert.ToInt32() gives you a loud failure instead of a corrupted value passing silently through your algorithm. A thrown exception during testing beats a wrong answer during an interview.
When long Isn't Enough: BigInteger
For exact arithmetic beyond long's range, BigInteger from System.Numerics has you covered:
using System.Numerics; BigInteger factorial = 1; for (int i = 1; i <= 100; i++) { factorial *= i; } // 100! is 158 digits. BigInteger handles it without complaint.
Operations are slower than fixed-width types since there are no hardware instructions for 158-digit arithmetic. But System.Numerics is available on virtually every interview platform, and BigInteger is completely fair game when the problem genuinely exceeds 64-bit range.
Floating-Point Is a Different Problem
double and float overflow to PositiveInfinity rather than wrapping, so they don't silently produce negative values. The issue with floating-point is precision, not wrapping:
Console.WriteLine(0.1 + 0.2); // 0.30000000000000004 Console.WriteLine(0.1m + 0.2m); // 0.3
decimal is a 128-bit type with 28-29 significant digits, exact for base-10 fractions, and 10-20x slower than double. Most interview problems use integers. If a problem mentions "cents" or "exact decimal," use decimal with the m suffix on literals.
Five Patterns That Still Catch Candidates
1. Multiplying two ints and storing in a long.
long result = a * b; // wrong if a and b are int long result = (long)a * b; // correct
The overflow happens during multiplication, before the assignment. Cast one operand first.
2. Using int for a running sum of large inputs. If you're accumulating up to 10^5 values each up to 10^9, the sum can reach 10^14. Initialize your accumulator as long:
long sum = 0; foreach (int val in nums) sum += val;
3. Checking for overflow after the fact.
// Wrong: a + b may already be corrupt if (a + b > int.MaxValue) ... // Correct: check before the operation if (a > int.MaxValue - b) ...
4. Assuming right shift fills zeros. >> on a signed integer is arithmetic (sign-extending). -1 >> 1 is still -1. For a logical shift that always fills with zeros, use >>> (C# 11+):
int x = -1; Console.WriteLine(x >> 1); // -1 Console.WriteLine(x >>> 1); // 2147483647
5. Trusting that long is always enough for factorials. 20! fits. 21! doesn't. If the problem expects exact values for n >= 21, you need BigInteger.
Quick Reference
| Situation | What to Use |
|---|---|
| Values up to ~2.1 × 10^9 | int |
| Values up to ~9.2 × 10^18 | long |
| Products of two large ints | (long)a * b |
| Binary search midpoint | left + (right - left) / 2 |
| Factorials of n >= 21 | BigInteger |
| Exact decimal fractions | decimal |
| General floating point | double |
| Negative modulo | ((a % m) + m) % m |
| Safe narrowing conversion | Convert.ToInt32() |
Key values to have locked in memory:
int.MaxValue = 2,147,483,647 (~2.1 × 10^9)
int.MinValue = -2,147,483,648
long.MaxValue = 9,223,372,036,854,775,807 (~9.2 × 10^18)
long.MinValue = -9,223,372,036,854,775,808
Practice Under Pressure
Knowing the rules is the easy part. The hard part is catching the bug while you're narrating your approach, thinking about algorithm correctness, and watching an interviewer scribble notes. SpaceComplexity runs voice-based DSA mock interviews where an AI will probe when your intermediate values look suspicious, the same way a real interviewer raises an eyebrow when you write int sum before accumulating ten million values. Catching overflow verbally, mid-explanation, is a different skill from catching it in a code review.
For a broader look at C# interview traps beyond arithmetic, C# Coding Interview Gotchas: What Compiles and Still Fails covers footguns across collections, null handling, and LINQ evaluation. And for the general overflow story across types and languages, What Is Integer Overflow and Underflow? is the conceptual foundation.
Further Reading
- Checked and Unchecked Statements in C# (Microsoft Docs)
- System.Numerics.BigInteger (Microsoft Docs)
- Integral Numeric Types in C# (Microsoft Docs)
- Integer Overflow (Wikipedia)
- Two's Complement (Wikipedia)