Palindromes are fascinating character sequences in a string. A palindrome is a string which reads the same when read from either of the ends. This post in particular talks about palindromic sub sequences.
To know more about a sub sequence, please check my post on Longest Common Sub sequence.
A palindromic sequence means a sequence of characters which is a palindrome. Now, we must understand it clearly that we are talking about a sub sequence and not a substring.
Understanding the Longest Palindromic Subsequence problem better
It is really easy to say if a string is a palindrome. We s...

Read More
## Binary Tree Linking Neighbors

Introduction
Problem Statement : Given a regular binary tree with left, right and peer node pointers. The left and the right pointers are already populated. We need to make the peer pointer point to the next right neighbor on the same level.
Understanding the problem
Here is a diagram to explain what is needed. The red peer pointer points to the immediate right neighbor if the neighbor exists.
Approach 1
The first solution which comes in mind is to do a custom, level order traversal as done in the post. And, in each iteration of the inner loop, just link each of the node to the next in ...

Read More
## Linked List Batch Reversing

Introduction
As I am writing this at 2 AM, this is going to be a small post but definitely an interesting one. This is mostly asked in interviews and is related to my previous post for Reversing a LinkedList.
Understanding the problem statement
Given a singly linked list L and a number k, we need to reverse each batch of k elements in the linked list. Here is a pictorial representation for the same.
For the below input linked list and for k equals to 3, you can see the output linked list under it. Each of the triplets are reversed.
Approach
We need to use our reverse linked list func...

Read More
## Topological Ordering for Graphs

Introduction
The topological ordering for graphs get many applications where the nature of the problem is ordered sequential processing. Few of the problems in this category are as follows:
Dynamic Linking/Loading of programs while building and execution.
Deciding the pre-requisites in a course structure.
Job Scheduling in Processors or Assembly Line Processing.
Building an Alien Dictionary from given words.
There are many more but I am sure you got the idea. When we say topological ordering of graph, we are necessarily talking about the ordering of the vertices of the graph.
Un...

Read More
## Matrix Transformation to make rows and columns zero

Introduction
Another interview question about matrices. Matrices have been of great interest in programming and Linear Algebra and a matter of study for most of the programmers. The problem statement follows:
"Given a matrix A of size M * N where M is the number of rows and N is the number of columns and each of its cell A[i][j] is populated with either zero or one. Transform the matrix such that if there exists a cell A[i][j] = 0 then the row i and column j is zero in the resultant matrix".
You must not use extra space and try to solve it in lowest possible running time.
Understanding...

Read More
## Left Hand Projection of a Tree

Introduction
Another interesting interview question, this question has many forms. One of the form is print the left hand projection of a tree or right hand projection of a tree or so on. Also, this problem can be specialized for a binary tree, ternary tree or n-ary tree, it really doesn't matter, the concept is the same.
We will just try and print the left hand projection and I will add a note on how to print the right had project and you can try that yourself.
Understanding the problem - Left Hand Projection of a Tree
Let us consider the below tree for our problem statement.
What is...

Read More
## Filling Wine Glasses Interview Question

Introduction
This is another interesting interview question and took me sometime to think for an answer. The problem statement goes as follow:
"There are wine glasses stacked such that the top level has one glass, the second level has two glasses, the third one has three glasses and so on.
Imaging that the levels are deep enough. Each of the glass in the stack has volume u. Now given a jug of volume V full of wine such that V >= u, find the lowest level where wine can reach."
Analyzing the problem statement
Here is a supporting image for the problem statement.
Now I pick up the ju...

Read More