Algorithmic problem solving Toolbox for Java
Bootcamp

Programming interviews are hard. If you are interviewing at top companies like Google, Amazon or Facebook you will be asked difficult problems: your interviewer is there to understand if you are better than most of the engineers that are currently working at the company.

As counter intuitive as it may seem, however, great value in your interview preparation comes from mastering the very basics of programming. Code fluency in your language of choice is a fundamental skill that is evaluated during interviews, and you can master this skill with very little effort and preparation. Think about it: when you are at the whiteboard trying to solve a very hard dynamic programming or recursive problem the last thing you want to do is stressing about the fact that you don’t remember the correct API to generate a random number, or how to iterate over the keys of a HashMap.

Practicing programming fluency is just as important as practicing solving problems, and it will greatly help you to succeed in your interview by giving you the piece of mind you need to concentrate on the problem at hand. In this post we focus on the Java language but we will be publishing similar posts for other languages as well.

Iterate a string one character at a time
void iterateString(String str) {
    for (char c : str.toCharArray()) {
        System.out.println(c);
    }
}
Iterate array from start and end
void iterateFromBothEnds(int[] arr) {
    int start = 0;
    int end = arr.length - 1;

    while (start <= end) {
        if(start == end) {
            System.out.println(arr[start]);
        } else {
            System.out.println(arr[start]);
            System.out.println(arr[end]);
        }
        start++;
        end--;
    }
}
Iterate HashMap
void iterateHashMap(HashMap<String, Integer> map) {
    // Iterate through each key
    for (String key : map.keySet()) {
        System.out.println(map.get(key));
    }

    // iterate through each value
    for (int value : map.values()) {
        System.out.println(value);
    }

    // Iterate one entry at a time
    for(HashMap.Entry<String, Integer> entry : map.entrySet()) {
        System.out.println(entry.getKey());
        System.out.println(entry.getValue());
    }
}
DFS
void dfs(BTNode node) {
    if (node == null) {
        return;
    }

    dfs(node.left);
    System.out.println(node.data);
    dfs(node.right);
}
BFS
void bfs(BTNode root) {
    Deque<BTNode> queue = new LinkedList<BTNode>();
    queue.add(root);

    while(!queue.isEmpty()) {
        BTNode node = queue.remove();
        System.out.println(node.data);

        if (node.left != null)
            queue.add(node.left);

        if (node.right != null)
            queue.add(node.right);
    }
}
int binarySearch(int[] A, int k) {

    int start = 0;
    int end = A.length-1;

    while (start <= end) {
        int mid = Math.floor(start+end)/2);

        if (A[mid].equals(k) {
            return mid;
        } else {
            if (A[mid] < k) {
                end = mid-1;
            } else {
                start = mid+1;
            }
        }
    }

    return -1;
}

Liked this article? Share it with others: