Posts

Given an integer n, write a function that returns count of trailing zeroes in n!. Examples: Input: n = 5 Output: 1 Factorial of 5 is 20 which has one trailing 0. Input: n = 20 Output: 4 Factorial of 20 is 2432902008176640000 which has 4 trailing zeroes. Input: n = 100 Output: 24

Void pointers: Difference between c and c++

I'm trying to understand the differences between C and C++ with regards to void pointers. the following compiles in C but not C++ : int * p = malloc ( sizeof ( int )); However, here: void foo ( void * vptr ) { } int main () { int * p = ( int *) malloc ( sizeof ( int )); foo ( p ); return 0 ; } Both C++ and C compile it with no complain. Why?  

Number is palindrome or not

Determine whether a number is palindrome or not without using extra space. Don't forget to mention whether your Algo will work for negative numbers or not.

Sum of four elements equals X

Given an array of integers, find all combination of four elements in the array whose sum is equal to a given value X. For example, if the given array is {10, 2, 3, 4, 5, 9, 7, 8} and X = 23, then your function should print “3 5 7 8″ (3 + 5 + 7 + 8 = 23). Note: Numbers should not be overlapping. eg(3,5,7,4) will generate 3+5=8 and 5+7=12 , for sum equals 20, 5 is overlapping.

All possible IP Address combinations

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: Given "25525511135" , return ["255.255.11.135", "255.255.111.35"] . (Order does not matter)

Difference in program size

What is the difference in terms of program size in case of these two types of declarations int a[100]; or, int a[100]={10};

Exchange gold coins

In a monetary system each coin has an integer number written on it. A coin ' n ' can be exchanged in a bank into three coins: n/2 , n/3 and n/4 . But these numbers are all rounded down. You can also sell coins for dollars and their exchange rate is 1:1. You have one gold coin. What is the maximum amount of dollars you can get for it? eg: for a coin with value 120 you can get 144 dollars. Suggest an algorithm to solve the problem.

[Adobe]Search for an element in unsorted array

The successive elements of an unsorted array differ by either + 1 or -1. How will u search for an integer k in this array ? eg: 4,5,4,4,5,6,7,8,7 for each element 'n' the next element is always n+1 or n-1.

Maximum sum 2-d subarray

Image
Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle . A sub-rectangle is any contiguous sub-array of size or greater located within the whole array. As an example, the maximal sub-rectangle of the array: is in the lower-left-hand corner: and has the sum of 15.

[Amazon]Two unsorted array form same BST

Given two unsorted array, check if both will create the same bst. eg: 2, 1, 4, 0 and 2, 1, 0, 4 will both form same BST. 2 / \ 1 4 / 0 but, 2,1,4,0 and 2,0,1,4 will not.

[AMAZON] Subtree with largest sum

Given a binary tree, every node has a int value, return the root node of subtree with the largest sum up value.

[AMAZON] Vertical sum of nodes

Given a Binary Tree, find vertical sum of the nodes that are in same vertical line. Print all sums through different vertical lines. Examples: 1 / \ 2 3 / \ / \ 4 5 6 7   The tree has 5 vertical lines Vertical-Line-1 has only one node 4 => vertical sum is 4 Vertical-Line-2: has only one node 2=> vertical sum is 2 Vertical-Line-3: has three nodes: 1,5,6 => vertical sum is 1+5+6 = 12 Vertical-Line-4: has only one node 3 => vertical sum is 3 Vertical-Line-5: has only one node 7 => vertical sum is 7 So expected output is 4, 2, 12, 3 and 7

[Amazon]Flatten a Linked-List

You are given a singly link-list such that each node of this list is also a head of another link list of the same type. So, suggest an algorithm to flatten the linked-list in single level link-list. struct node { void *data; /* could be anything */ struct node *next; struct node *down; };  

Implement tail utility

How will you implement tail utility in c/c++ without using inbuilt tail command. Note: tail -n <filename> will output the last 'n' lines of the file.

Find the Least Length Path Which Sums to S

Given a BST and a number 'N' there can be several paths from root of the bst to leaves such that sum of data of all the nodes will make number 'N'. Suggest an efficient algorithm to find out the path of least length.

[MS]Form Largest Number from given number

Write a function to make the largest number from the digits of a given number. Example. number: 32441 Output: 44321

Convert a Binary Tree To hold Children Sum Property

Given an arbitrary binary tree, convert it to a binary tree that holds Children Sum Property. You can only increment data values in any node. For example, the below tree doesn’t hold the children sum property, convert it to a tree that holds the property. 50 / \ / \ 7 2 / \ /\ / \ / \ 3 5 1 30

Connect nodes of a binery tree at same Level

Write a function to connect all the adjacent nodes at the same level in a binary tree. Structure of the given Binary Tree node is like following. struct node {    int data;    struct node* left;    struct node* right;    struct node* nextRight; } Initially, all the nextRight pointers point to garbage values. Your function should set these pointers to point next right for each node. You can use only constant extra space. Consider this example for your solution: 1 / \ 2 3 / \ / \ 4 5 6 7 / \ / \ 8 9 10 11  nextRight for node with value 9 will be 10 and not NULL.

perfect cubes

It is possible, to find integers greater than 1 that satisfy the "perfect cube" equation a^3 = b^3 + c^3 + d^3 (e.g. a quick calculation will show that the equation 12^3 = 6^3 + 8^3 + 10^3 is indeed true). This problem requires that you write a program to find all sets of numbers {a,b,c,d} which satisfy this equation for a <= N.

Euclidean Distance

You are given a multiset of points on the plane with integer coordinates. Find the maximum distance between two points from this multiset.