Friday, August 19, 2011
Given a string, find the length of the longest substring without repeating characters.
For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1.
Labels:Data
Google Interview
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99. Find the largest palindrome made from the product of two 3-digit numbers.
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99. Find the largest palindrome made from the product of two 3-digit numbers.
Labels:Data
D.E Shaw
,
Google Interview
Thursday, August 18, 2011
Design an algorithm to perform operation on an array Add(i,y)-> add value y to i position sum(i) -> sum of first i numbers we can use additional array O(n) and worst case performance should be O(log n) for both operation
Design an algorithm to perform operation on an array
Add(i,y)-> add value y to i position
sum(i) -> sum of first i numbers
we can use additional array O(n) and worst case performance should be O(log n) for both operation
Practice Problem For Binary Index Tree :
http://problemclassifier.appspot.com/index.jsp?search=BIT&usr=
http://problemclassifier.appspot.com/index.jsp?search=tree&usr=
Add(i,y)-> add value y to i position
sum(i) -> sum of first i numbers
we can use additional array O(n) and worst case performance should be O(log n) for both operation
Practice Problem For Binary Index Tree :
http://problemclassifier.appspot.com/index.jsp?search=BIT&usr=
http://problemclassifier.appspot.com/index.jsp?search=tree&usr=
Labels:Data
Amazon Interview
,
Binary Index Tree
,
Google Interview
,
Segment Tree
Given an Array of Interegers , NUmber of Unique Binary Searchj Trees Can be Made from this values are ?
you are building an N node binary search tree with the values 1..N. How many structurally different binary search trees are there that store those values? Write a recursive function that, given the number of distinct values, computes the number of structurally unique binary search trees that store those values. For example, countTrees(4) should return 14, since there are 14 structurally unique binary search trees that store 1, 2, 3, and 4.
Labels:Data
Amazon Interview
,
Google Interview
Saturday, August 13, 2011
Fix the ClassThatNeedsFixing implementation so that the critical section is protected.
I have two methods of an object, and they each access a critical section of code. I want to restrict access to the section so that in one method, I allow multiple threads to access the critical section. In the other method, I want only one thread to have access. If a caller calls the second method, it should lock out all clients from accessing the critical section in either of the two functions. Here is the basic structure of the class:
class ClassThatNeedsFixing
{
public:
// Will allow many concurrent threads through, unless there is a
// call to the other method.
void AllowMany() {
// Here is the critical section that must be protected
...
}
// Will lock out any client, including callers to the other method.
void AllowOne() {
// Here is the critical section that must be protected
...
}
private:
// Assume there are members here that need protecting
// above.
...
};
In order to solve this problem, you are provided with two classes: Mutex and Semaphore. They have the standard behavior of the concepts that share their class names. Here are the public interfaces for each of these classes:
class Mutex
{
public:
Mutex();
void Acquire();
void Release();
};
class Semaphore
{
public:
// At it's creation, one can specify the count
// of the semaphore.
Semaphore(unsigned int count);
void Acquire();
void Release();
};
Fix the ClassThatNeedsFixing implementation so that the critical section is protected.
Your solution will be graded on flexibility and robustness (i.e., we should be able to re-use your solution in a generic case and it should be exception safe). You are allowed to create as many classes/objects/templates/etc that you need. Feel free to use the STL if necessary. Document your code as you would for real-world maintainability.
class ClassThatNeedsFixing
{
public:
// Will allow many concurrent threads through, unless there is a
// call to the other method.
void AllowMany() {
// Here is the critical section that must be protected
...
}
// Will lock out any client, including callers to the other method.
void AllowOne() {
// Here is the critical section that must be protected
...
}
private:
// Assume there are members here that need protecting
// above.
...
};
In order to solve this problem, you are provided with two classes: Mutex and Semaphore. They have the standard behavior of the concepts that share their class names. Here are the public interfaces for each of these classes:
class Mutex
{
public:
Mutex();
void Acquire();
void Release();
};
class Semaphore
{
public:
// At it's creation, one can specify the count
// of the semaphore.
Semaphore(unsigned int count);
void Acquire();
void Release();
};
Fix the ClassThatNeedsFixing implementation so that the critical section is protected.
Your solution will be graded on flexibility and robustness (i.e., we should be able to re-use your solution in a generic case and it should be exception safe). You are allowed to create as many classes/objects/templates/etc that you need. Feel free to use the STL if necessary. Document your code as you would for real-world maintainability.
Labels:Data
Google Interview
Friday, August 12, 2011
Given two strings find the combination of strings which can be interleaved
e.g. i/p: if "AB" and "CD" are two strings
o/p:
ABCD
ACBD
ACDB
ACBD
CABD
CDAB
o/p:
ABCD
ACBD
ACDB
ACBD
CABD
CDAB
Labels:Data
Global Scholar
Thursday, August 11, 2011
find the kth largest sum(a+b) possible where a in A, and b in B.
Given 2 Sorted arrays A, B in decreasing order of size m and n respectively and a number k, such that 1 <= k <= m*n. You need to find the kth largest sum(a+b) possible where a in A, and b in B.
Algorithm:
Take 0th index value from 1st array & 1st index value from 2nd array store it in S1
Now 0th index value from 2nd array & 1st index value from 1st array , store in S2
Also stores starting & ending index of each array -take care-off
iniatlize maxsum variable to a[0]+b[0]
Keep checking until we found kth sum from array a & b where a is from 1st & b is from
if s1>s2 then increment array b lastindex untill its equal to length of this array & update maxsum=s1
else then increment array a lastindex until its equal to length of this array
maxsum=s2;
Finally Return maxsum;
2nd array.
maxsum=s2;
Finally Return maxsum;
#include<stdio.h> int k_sum(int A[],int B[],int m,int n,int k){ int indexA = 1; int indexB = 1; int i,s1,s2,sA=0,sB=0; int maxsum = A[0] + B[0]; for(i=0;i<k-1;i++) { s1 = A[sA] + B[indexB]; s2 = A[indexA] + B[sB]; if(s1 >= s2){ if(indexB == n-1){ indexB = sB + 1; ++sA; } else{ ++indexB; } maxsum = s1; } else{ if(indexA == m-1){ indexA = sA + 1; ++sB; } else{ ++indexA; } maxsum = s2; } } return maxsum; } int main(){ int A[5]= {10,9,6,4,3}; int B[6] = {15,13,12,10,78,5}; int k=10; printf("%d \n",k_sum(A,B,5,6,10)); return 0; }
Time Complexity O(K) K=M*N M,N are length of length of array s1,s2
Space Complexity O(1)
https://ideone.com/2IxZX
Run Here https://ideone.com/5Zy8B
Labels:Data
Amazon Interview
,
Google Interview
Wednesday, August 10, 2011
Subscribe to:
Posts
(
Atom
)