Coding Interview - Easy Problems

Last 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) {

    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.