Posts

Sizeof operator again

char * p = "Hello World" ; int a ; char b ; printf ( "%d\n" , sizeof ( p ++)); printf ( "%c\n" ,* p ); printf ( "%d" , sizeof ( a , b )); printf ( "%d" , sizeof ( b , a ));   Explain why p didn't get incremented and  what is the use of comma operator here.

[Adobe]Job scheduling problem

You are given N jobs with their start and end time mentioned.These jobs may have their timings overlapped.You have to suggest an algorithm such that maximum number of jobs can be performed in that given time interval.

[facebook]Number as sum of candidate numbers

Given a number, and a series of candidate numbers, print out all combinations, so that the sum of candidate numbers equals to the given number. Here order is not important, so don’t print the duplicated combination. e.g. target is 7 , candidate is 2 , 3 , 6 , 7 output should be 7 and 3 + 2 + 2 (but not print 2 + 3 + 2 , 2 + 2 + 3 )

[MS]GCD using bitwise operation

Suggest an algorithm to find gcd of two numbers faster than Euclidean method. Note: This works on bitwise operation.

[MS]Remove multiple spaces

Given a string with multiple delimiters in between words,you have to replace them with single space. eg:"       hello\t\t   hii    " is changed as " hello hii ".

Count trees formed of n nodes

If there are n nodes given ,how many structurally different binary trees can be formed. Note: You don't have to create trees out of n nodes given ,this is a counting problem.

[MS]Fill the rectangle with squares

Given a rectangle with known width and height, design an algorithm to fill the rectangle using n squares(n is integer given) and make sure in the result the wasting area is minimized. Length of square doesn't have to be integer. i.e, given width=3,height=2,n=5, one solution is that rectangle can be filled with five 1x1 squares and the wasting area is 1. Another solution could be filled with five 0.9x0.9 squares, but the wasting area is more than first solution.

[Qualcomm]Generate random number between 1 and 7

  Write a method to generate a random number between 1 and 7, given a method that generates a random number between 1 and 5. The distribution between each of the numbers must be uniform.

Equilibrium index of an array

Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in an arrya A: A[0] = -7, A[1] = 1, A[2] = 5, A[3] = 2, A[4] = -4, A[5] = 3, A[6]=0 3 is an equilibrium index, because: A[0] + A[1] + A[2] = A[4] + A[5] + A[6] 6 is also an equilibrium index, because sum of zero elements is zero, i.e., A[0] + A[1] + A[2] + A[3] + A[4] + A[5]=0

Delete nodes which have a greater value on right side

Given a singly linked list, remove all the nodes which have a greater value on right side. Examples: a)  The list 12->15->10->11->5->6->2->3->NULL should be changed to 15->11->6->3->NULL. Note that 12, 10, 5 and 2 have been deleted because there is a greater value on the right side.

Modify linked list into even and odd elements

Given a Linked List of integers, write a function to modify the linked list such that all even numbers appear before all the odd numbers in the modified linked list.  Also, keep the order of even and odd numbers same. Examples: Input: 17->15->8->12->10->5->4->1->7->6->NULL Output: 8->12->10->4->6->17->15->5->1->7->NULL

trucks with payloads

Given a fleet of 50 trucks, each with a full fuel tank and a range of 100 miles, how far can you deliver a payload? You can transfer the payload from truck to truck, and you can transfer fuel from truck to truck. Assume all the payload will fit in one truck.

first odd number in the dictionary

Each number from 1 to 10^10 are written out in english format (eg: "two hundred eleven","one hundred forty two") and then listed down in alphabetical order as in dictionary.Which is the first odd number in the list?

Divisible by 10

Given a stream of binary digits how can you tell if the number formed is divisible by 10.

Count number of occurrences of a number

Given a sorted array arr[] and a number x, write a function that counts the occurrences of x in arr[]. Expected time complexity is O(Logn) Examples: Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 2 Output: 4

Find the celebrity

You are in a party which has 'n' guests. One of them being a celebrity which meets the following criteria. 1. Celebrity doesn't know anyone 2. All others except the celebrity know atleast one person including the celebrity. You are allowed to ask any person - DO YOU KNOW X person. How many questions you need to identify the celebrity

Leaders in an array

Write a program to print all the LEADERS in the array. An element is leader if it is greater than all the elements to its right side. And the rightmost element is always a leader. For example int the array {16, 17, 4, 3, 5, 2}, leaders are 17, 5 and 2.

Two elements with minimum sum

Image
An Array of integers is given, both +ve and -ve. You need to find the two elements such that their sum is closest to zero. For the below array, program should print -80 and 85.

Rotation of array by d position

Image
Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements. Rotation of the above array by 2 will make array

Tie a rope around the earth

A fool wants to tie a rope around the earth. So he buys a rope of 40,000 KM and ties it around the world. His neighbour, also a fool, wants to do the same only he wants the rope on sticks 1 meter above the ground. How much more rope does he need?