Tag Archives: overflow

Algorithm Tips (2) – Avoiding Data Overflow

Today we talk about overflow. This is a very shy problem when interviewing. And it is also a very good way to distinguish whether you are a good programmer or just so-so programmer.

When I interviewed in a big company (I forgot its name, maybe eBay), the interviewer asked me to write a sum function. So easy?! In fact, No. You have to consider overflow for your solution.

Concept of Overflow and Underflow

First, let’s understand what’s overflow and what’s underflow. Overflow and underflow are related to a data type. Every data type has its own range. For example, int, in Java, you can use Integer.MIN_VALUE and Integer.MAX_VALUE to know its range. In Java arithmetic operators, it doesn’t report overflow and underflow problem and never throws an error exception. It just simply swallows it.

  • int operators:
    When the value of an operation is larger than 32 bits, then the low 32 bits only taken into consideration and the high order bits are discarded. And when its most significant bit(MSB) is 1 then the value is treated as negative.
  • floating point operators:
    Overflow will result in Infinity and underflow will result as 0.0.

Solutions to avoid overflow or underflow

Second, there are several ways to avoid it.

  • using long to replace int
  • using uint to replace int
  • using double to apply intermediate variable
  • use the mod method to avoid it, This is a very common solution in LeetCode, within the question, it already obviously reminds you to use mod 100000007 if the answer is very large.


Finally, I list my solution for LeetCode-576 (Out of Boundary Paths) (Here I don’t list the content of the question, you can read it from LeetCode’s website. ) to help you understand how to avoid overflow issue. Meanwhile, it is a good way to see how to use three dimensions array to fix the problem with dynamic programming method.

public class Solution {
  public int findPaths(int m, int n, int N, int i, int j) {
    final int MOD = 1000000007;
    if (N == 0) return 0;
    long[][][] dp = new long[m][n][N+1]; //k:1-N
    for(int ii=0; ii<m;ii++) {
      for(int jj=0; jj<n;jj++) {
        int count = 0;
        if (ii-1<0) count++;         if (ii+1>=m) count++;
        if (jj-1<0) count++;         if (jj+1>=n) count++;
        dp[ii][jj][1] = count;
    for(int k=2; k<=N;k++) {
      for(int ii=0; ii<m; ii++) {
        for(int jj=0; jj<n; jj++) {
          long count = 0;
          if (ii-1>=0) count += dp[ii-1][jj][k-1];
          if (jj-1>=0) count += dp[ii][jj-1][k-1];
          if (ii+1<m) count += dp[ii+1][jj][k-1];
          if (jj+1<n) count += dp[ii][jj+1][k-1];
          dp[ii][jj][k] = count%MOD;
    long rec = 0;
    for(int k=1; k<=N; k++) {
      rec += dp[i][j][k];
    return (int)(rec%MOD);

Another simple LeetCode question also requires us to consider overflow issue: Valid Binary Search Tree. Here is the solution which considers using Long to replace Integer to avoid overflow.

 * Definition for a binary tree node.
 * public class TreeNode {
 *   int val;
 *   TreeNode left;
 *   TreeNode right;
 *   TreeNode(int x) { val = x; }
 * }
public class Solution {
 public boolean isValidBST(TreeNode root) {
   return isValidBSTCheck(root, Long.MIN_VALUE, Long.MAX_VALUE);
 public boolean isValidBSTCheck(TreeNode root, long min, long max) {
   if (root == null) return true;
   if (root.val > min && root.val < max) {
     return isValidBSTCheck(root.left, min, root.val) && isValidBSTCheck(root.right, root.val, max);
   } else return false;