Given a series (2,3,4,5,6,8,9,10,12,15,...) where the elements are in multiple of 2,3 and 5. Find the nth element of this sequence

Problem: Assume given sequence : 2,3,4,5,6,8,9,10,12,15… All these numbers are multiples of either 2 , 3, or 5 (and no other numbers). Find the nth element of this sequence. (This is the interview question in flipkart) Solution: This can be thought in recursive terms. But to begin with, we can clearly see, 2.5=10 is part of series, but 2.7=14 is not part of series. So, while generation of numbers we have to make sure that divisor is not part of numbers like 7,11,13 etc. [Read More]

Stack of plates

**Question ** Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in real life, we would likely start a new stack when the previous stack exceeds some threshold. Implement a data structure SetOfStacks that mimics this. SetOfStacks should be composed of several stacks, and should create a new stack once the previous one exceeds capacity. SetOfStacks.push() and SetOfStacks.pop() should behave identically to a single stack (that is, pop() should return the same values as it would if there were just a single stack). [Read More]

Print nodes at k distance from root

Given a root of a tree, and an integer k. Print all the nodes which are at k distance from root. For example, in the below tree, 4, 5 & 8 are at distance 2 from root. 1 / \\ 2 3 / \\ / 4 5 8 Code: void printKDistant(node \*root , int k) { if(root == NULL) return; if( k == 0 ) { printf( "%d ", root->data ); return ; } else { printKDistant( root->left, k-1 ) ; printKDistant( root->right, k-1 ) ; } } Time Complexity: O(n) where n is number of nodes in the given binary tree. [Read More]

Find the time period with the maximum number of overlapping intervals

Problem There is one very famous problem. I am asking the same here. There is number of elephants time span given, here time span means, year of birth to year of death. You have to calculate the period where maximum number of elephants are alive. Example : 1990 - 2013 1995 - 2000 2010 - 2020 1992 - 1999 Answer is 1995 - 1999 Solution Pseudocode Split each date range into start date and end date. [Read More]