Split The Linked List

Given a linked list and a number n. Split the link list into two based on the following pattern input: 1->2->3->4->5->6->7->8->null and n=2 output: 1-2->3->4->null and 5->6->7->8->null input: 2->3->1->4->6->7->7->6->null and n=4 output: 2-3->null and 1->4->6->7->7->6->null return the right partition pointer. First note that pattern. For n=2 it is effectively partitioning the list into two halves. […]

Smallest Difference between 2 elements from 2 different array

Given two arrays. Find the smallest difference between two elements from both of the arrays. For example: A=[0, -6, 4, 6, 5, -2], and A=[-4, 8, 2, 3, 10, 9] then then the smallest difference between two elements of the two array is 1 with pairs (4, 3) from the arrays repectively. Approach 1: w/o […]

Sliding window min/max – Dynamic Programming

Given an array of integer A[] and the size of sliding window w. Assume that the window of size w starting from left keeps sliding by moving the window one element to right each time. Find the stream of sliding minimums in optimal way. A sliding minimum is the minimum element of current window. Let’s […]

Serialize and Deserialize Binary Tree

Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure. For example, one possible format of serialized tree […]

Search in a 2D Array or Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. Matrix can have two forms. Solve it for each form of the matrix. This matrix has the following properties: Version 1 Integers in each row are sorted from left to right. The first integer of each row is greater than […]

Search first occurrence of a duplicated element in a sorted array

Given an array of integer with duplicated elements. Find the first occurrence of a given number in the array. For example: A = [1, 1, 1, 2, 2, 3, 3, 3, 4]. So, first occurrence of 2 is in index 3, first occurrence of 4 is in index 8. The problem can be solved efficiently […]

Reverse Linked List – single pointer – in group of K

Reverse a link list using one/single pointer. We first try to device straightforward solution to reverse a linked list recursively. For example, given a linked list 1->2->3->4->5->null, how we can reverse this? Let’s start with straightforward solution using two temp pointers. We will basically keep a new pointer to append nodes from the list into […]