Algorithmic Problems and Solutions

Arrays

Two Pointers

Two Number Sum Problem solution in JavaGiven an array of integers, return the indices of the two numbers whose sum is equal to a given target.

Three Number Sum Problem (Find all triplets with the given sum)Given an array of integers, find all triplets in the array that sum up to a given target value.

Find the pair with the smallest difference in two unsorted arraysGiven two nonempty arrays of integers, find the pair of values (one value from each array) with the smallest (nonnegative) difference.

Remove duplicates from sorted arrayGiven a sorted array, remove the duplicates from the array inplace such that each element appears only once, and return the new length.

Remove duplicates from sorted array IIGiven a sorted array, remove the duplicates from the array inplace such that each element appears only once, and return the new length.

Max Consecutive Ones IIIGiven an array A of 0's and 1's, and a value K which indicates that you may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1's.


Binary Search

Binary Search Implementation (Iterative and Recursive)Binary search is the most efficient algorithm for searching an element in a sorted array. It has a runtime complexity of O(log n).

Search an element in a sorted 2d MatrixWrite an efficient algorithm that searches for a value in a sorted m x n matrix. The matrix has these properties: the integers in each row are sorted from left to right, and the first integer of each row is greater than the last integer of the previous row.

Search an element in a Rotated Sorted ArraySuppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [1, 4, 6, 8, 11, 13, 15] might become [8, 11, 13, 15, 1, 4, 6]). You are given a target value to search. If found in the array return its index, otherwise return 1. You may assume no duplicate exists in the array. Your algorithm's runtime complexity must be in the order of O(log n).


Linked Lists

Reverse a Singly Linked ListReverse a Singly Linked List solution in Java. To reverse a singly LinkedList, we can keep two pointers  one pointing to the currentNode and another pointing to the previous node. Now, we can iterate over the LinkedList, and for every iteration  we reverse the pointers (Point currentNode.next to prevNode), and move the two pointers to their next node in the LinkedList.

Add two numbers represented using Linked ListsYou are given two nonempty linked lists representing two nonnegative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Merge Two Sorted Linked ListsMerge two sorted linked lists and return it as a new list. The new list should also be sorted.


Stacks and Queues

Dynamic Programming

Heaps and Maps

Trees

Convert Sorted Array to Balanced Binary Search TreeGiven an array where elements are sorted in ascending order, convert it to a height balanced BST. A heightbalanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Symmetric Binary Tree (Mirror image of itself) problemGiven a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

Identical Binary Trees (Same Tree) problemGiven two binary trees, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical and the nodes have the same value.

Find the closest element to a target value in a binary search treeGiven a nonempty binary search tree and a target value, find the value in the BST that is closest to the target.

Given a binary tree, determine if it is heightbalancedGiven a binary tree, determine if it is heightbalanced. A heightbalanced binary tree is defined as: a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
