Posts

Showing posts with the label Programming

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)

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.

[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.

Pair of Numbers

Let's assume that we have a pair of numbers ( a ,  b ). We can get a new pair ( a  +  b ,  b ) or ( a ,  a  +  b ) from the given pair in a single step. Let the initial pair of numbers be (1,1). Your task is to find number k , that is, the least number of steps needed to transform (1,1) into the pair where at least one number equals n .

[AMAZON] Stock purchase problem

Suppose we are given an array of n integers where each index represent stock prices on a single day. We want to find a pair (buyDay, sellDay), with buyDay ≤ sellDay, such that if we bought the stock on buyDay and sold it on sellDay, we would maximize our profit. Clearly there is an O(n 2 ) solution to the algorithm by trying out all possible (buyDay, sellDay) pairs and taking the best out of all of them. However, is there a better algorithm, perhaps one that runs in O(n) time?

[AMAZON]Longest palindrome substring

Given a string S, find the longest palindromic substring in S.

[AMAZON]printKDistanceNodes

Image
You are given a function  printKDistanceNodes   which takes in a root node of a binary tree, a start node and an integer  K .  Complete the function to print the value of all the nodes (one-per-line) which are a  K  distance from the given start node in sorted order. Distance can be upwards or downwards. Example: Sample Input: Root node: 5 Given start node: 8 Distance (K): 1 Sample Output: 5 6 9

[AMAZON]Arrange Array elements

In given array of elements like [a1,a2,a3,..an,b1,b2,b3,..bn, c1,c2,c3,...cn] Write a program to merge them like   [a1,b1,c1,a2,b2,c2,...an, bn,cn]. PS: Do it without using extra memory Sample Testcases: Input #00: {1,2,3,4,5,6,7,8,9,10,11,12} Output #00: {1,5,9,2,6,10,3,7,11,4,8,12} Explanation: Here as you can notice, the array is of the form {a1,a2,a3,a4,b1,b2,b3,b4,c1, c2,c3,c4}

Find the two repeating elements in a given array

You are given an array of n+2 elements. All elements of the array are in range 1 to n. And all elements occur once except two numbers which occur twice. Find the two repeating numbers. For example, array = {4, 2, 4, 5, 2, 3, 1} and n = 5 The above array has n + 2 = 7 elements with all elements occurring once except 2 and 4 which occur twice. So the output should be 4 2.