Data Structures
    Overview
    Number
    String
    Array
    Linked List
    Stack
    Hash
    Tree
    Trie
    Advanced Data Structure
    Probabilistic Data Structures
    Big O

Coding Interview - Easy Problems

Updated: 2021-12-15

Calculate sum of a list of Integers

public int getSum(List<Integer> s) {
    BigInteger sum = BigInteger.ZEOR;
    for (Integer i : s) {
        sum.add(BigInteger.valueOf(i));
    }

    if (sum.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0 || sum.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0 ) {
        throw new RuntimeException("Sum exceeds the limit of Integer");
    }

    return sum.intValue();
}

calculate sum in parallel: use subList to create a view of the list without making copies

subList(int fromIndex, int toIndex)

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.