Showing posts with label Facebook Interview. Show all posts
Showing posts with label Facebook Interview. Show all posts
Sunday, October 13, 2013
Two players play the following game: they pick a random number N (less than 2 billion) then, starting from 1, take turns multiplying the number from the previous turn with either 2 or 9 (their choice). Whoever reaches N first wins. The candidate should write a function that given N decides who wins (first or second player)?
Labels:Data
Facebook Interview
Saturday, June 29, 2013
Print the objects of an array in the order of decreasing frequency. PS: Please make sure order of object should remain same
E.g. if array is of integer is given as 5 2 2 8 5 6 8 8, then we should get 8 8 8 5 5 2 2 6 , here freq. of 5 and 2 are same but 5 comes before 2 in array thus its should come before in output as well.
Here is the working code , let me know if it will fail for any test case and if we can improve it?
/********************************************************Time Complexity : O(n)+O(nlogn)+O(n)=O(nlogn)Space Complexity : Auxallery Space O(2n)= O(n)*********************************************************/Run Here http://ideone.com/FQ65xM
Labels:Data
Amazon Interview
,
Facebook Interview
,
Google Interview
Monday, February 25, 2013
'K' number of char arrays of different length are given, find Cartesian product of them in optimal way & give complexity.
Source : commented by user yen
Labels:Data
Facebook Interview
Given n, output the numbers from 0 to 2^n-1 (inclusive) in n-bit binary form, in such an order that adjacent numbers in the list differ by exactly 1 bit.
Source : Heard from Rahul, CSE, BITS ,2K10
Labels:Data
Facebook Interview
,
Gray Code
Wednesday, August 15, 2012
Write an algorothm to find the position of rightmost set bit in binary representation of number
Example
Number 19, Binary Representation 010011
Answer Position of right most set bit 1
Here is an order of log(X) algorithm. We can conclude from 2's complement form that "a number can be converted to 2's complement form by complementing each bit from right most set bit". For example, -7 can be obtained in the following way (assuming 8 bit size)
8 = 00001000
-8 = 11111000
If we perform ANDing between x and -x we left with right most set bit. All this takes O(1) time. Now use binary search [ O(log(x)) ] to figure out which bit is set. Given below is code.
int
getPosition(unsigned x)
{
// ASSUMPTION: x will not be zero
// left, right and mid position
int
l = 0, r = 33, mid = 0;
// temp value
unsigned temp;
// Iterate till we find the bit position
while
(l < r)
{
// ((r - l) >> 1) + l - Is more effective??
mid = (l + r) >> 1;
// Set the most possible significant bit
temp = (1 << (mid - 1));
if
(temp == x)
{
break
;
}
else
if
(temp < x)
{
// Bit is in the higher half
l = mid;
}
else
{
// Bit is in the lower half
r = mid;
}
}
// Return mid itself if bits
// are positioned from 1 to 32
return
(mid-1);
}
int
getRightMostSetBit(unsigned x)
{
// Return 0 if x is zero
int
position = 0;
// Avoid multiple return statements
if
(x != 0)
{
// Make the integer passes as least power of 2
// Call the position locator
position = getPosition(x & -(
signed
)x);
}
return
position;
}
Source Heard from My Friend Venki
Labels:Data
Facebook Interview
,
Google CodeJam
Subscribe to:
Posts
(
Atom
)