In this post we are going to look at data structures for storing stream of numbers in a way such that the following operations can be efficiently performed (although the data structures we are going to visit here can do a lot more):

- Current minimum and maximum numbers.
- Median of all numbers seen so far.
- Smallest (Largest) number greater (smaller) than a given number.
- Count of all smaller (or larger) numbers given a number.
- Binary search on numbers seen so far.

For 1 and 2, we can use a Min-Heap and a Max-Heap. 3 and 4 are special cases of Binary Search (5). But all of the above use cases can be solved efficiently if the numbers are sorted. …

In the last post on binary search optimization, we found out that we can solve optimization problems using binary search under certain conditions. In this post we are going to look at another class of optimization problems that can again be solved using binary search technique. This class of problem is called the Longest Increasing Subsequence. I fondly call it the Russian Doll problem.

In its most simplest form, we are given an array A of size N. Each element of the array A is a real number. …

Binary Search is probably one of the most ‘interesting’ algorithm from our high school and sophomore college computer science course. But most of us have encountered binary search either in the context of searching in a sorted array or some tweaked versions of similar problem:

- Find an element in a rotated sorted array (with and without duplicates allowed).
- Find an element in the 2D sorted array, only rows sorted, only columns sorted and both row and column sorted versions.

While these problems are interesting enough to puzzle even the most well prepared interview candidates. But in this post I will go into a different direction with binary search. …

In this post we are going to look at a problem that is very relevant from a network analysis point of view. In a circuit network of wires, we would like to identify vulnerable connections that could be a single point of failure. Single point of failures like these are known as Articulation Points.

In a social network we would like to identify people who sits at the vulnerable points and hold together multiple sub-networks. Targeting these people for marketing offers and promotions and preventing them from churning will ensure that the network is still connected. …

In this post we are going to look at one of the ‘simplest’ Dynamic Programming variation — Bitmasking. When I say ‘simplest’ I mean that ‘Bitmasking + DP’ problems are easy to identify and also most bit-masking problems have a more or less similar approach.

Some of the common traits of Bitmasking problems are:

- Path finding problems — Enumerate all possible paths or paths based on certain conditions.
- For a given input array A, depending in which order we go through the elements of the array A our objective function will change.
- They require exponential runtime.
- For folks into competitive programming will understand that problems with constraint on N (≤ 20) (i.e. size of array) hints towards bitmasking + DP. …

[1] Number of Ways to Wear Different Hats to Each Other — There are `n`

people and 40 types of hats labeled from 1 to 40.

Given a list of list of integers `hats`

, where `hats[i]`

is a list of all hats preferred by the `i-th`

person.

Return the number of ways that the n people wear different hats to each other.

Since the answer may be too large, return it modulo `10^9 + 7`

.

Example:

**Input:** hats = [[3,5,1],[3,5]]

**Output:** 4

**Explanation: **There are 4 ways to choose hats

(3,5), (5,3), (1,3) and (1,5)

(https://leetcode.com/problems/number-of-ways-to-wear-different-hats-to-each-other/)

Approach — Use Bitmasking + Dynamic Programming. …

[1] Flatten a Multilevel Doubly Linked List — You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure, as shown in the example below.

Flatten the list so that all the nodes appear in a single-level, doubly linked list. You are given the head of the first level of the list.

Example:

**Input:** head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]

**Output:** [1,2,3,7,8,11,12,9,10,4,5,6]…

[1] Find Longest Awesome Substring — Given a string `s`

. An awesome substring is a non-empty substring of `s`

such that we can make any number of swaps in order to make it palindrome.

Return the length of the maximum length awesome substring of `s`

.

Example:

**Input:** s = "3242415"

**Output:** 5

**Explanation:** "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.

(https://leetcode.com/problems/find-longest-awesome-substring/)

Approach — A substring is palindrome if all the numbers occurs even number of times or only one number occurs odd number of times.

Let ‘n’ represent the integer corresponding to the bit vector whether count of the digit k is even or odd. …

[1] Remove Nth Node From End of List — Given a linked list, remove the n-th node from the end of list and return its head.

Example:

Given linked list:1->2->3->4->5, andn = 2.After removing the second node from the end, the linked list becomes1->2->3->5.

(https://leetcode.com/problems/remove-nth-node-from-end-of-list/)

Approach — Use 2 pointer approach. One pointer is n+1 nodes ahead of the other. When the forward pointer reaches the end, the backward pointer’s next node is to be removed.

[2] Reverse Nodes in k-Group — Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. …

[1] Largest Perimeter Island — Find the largest perimeter of an island given a 2 dimensional array of 1’s and 0’s representing land and water respectively.

Land = 1

Water = 0

Island = contiguous 1’s that are adjacent to other 1’s to the left, right, up, or down

Edge of an island = any 1 that is adjacent to a 0 OR grid boundary to the left, right, up, or down

Perimeter = total # of 1’s on the edge of an island

Example:

`Input:`

[[1, 0, 1, 1, 1],

[1, 0, 1, 1, 1],

[0, 1, 0, 1…

About