vasa.v03
BAN USER- 0of 0 votes
AnswersImplement a function which accepts a number and returns top 10 big numbers the function is called with so far;
- vasa.v03 in India
If we call the function with 1.. to 100 , for the call function(100) the function will return 91 to 100 in reverse order since they are top 10 biggest number so far| Report Duplicate | Flag | PURGE
British Telecom Software Engineer Algorithm - 0of 0 votes
AnswersHow to maintain a Singleton in clustered environment
- vasa.v03 in India| Report Duplicate | Flag | PURGE
Ebay Member Technical Staff Java - 0of 0 votes
AnswersProgram to calculate a power n
- vasa.v03 in United States| Report Duplicate | Flag | PURGE
Java - 0of 0 votes
AnswersProgram to calculate a pow n...
I gave a answer with O(logN)
- vasa.v03 in Indiapublic static long exp(int a , int n) { boolean is_N_Odd = false; if ( n % 2 > 0 ) { n++; is_N_Odd =true; } if ( n == 2 ) return a * a; else if (n == 1 ) return a; else { long calc = exp (a , n/2) ; if ( is_N_Odd ) return calc * calc /a; else return calc * calc ; } }
| Report Duplicate | Flag | PURGE
Ebay Member Technical Staff Java - 0of 0 votes
AnswerHow to maintain locks/monitors in clustered environment ?
- vasa.v03 in India| Report Duplicate | Flag | PURGE
Ebay Member Technical Staff Java - 0of 0 votes
AnswerHow to manage a singleton in clustered environment ?
- vasa.v03 in India| Report Duplicate | Flag | PURGE
Ebay Member Technical Staff Java - 0of 0 votes
AnswersA Query to delete duplicate rows ....
- vasa.v03 in India
Let say i have a table
Employee - Emp_id,Emp_name,Emp_email
and there is no primary/unique key
Emp_id,Emp_name,Emp_email
1 abc a@b.com
1 abc a@b.com
2 abc a@b.com
Retain only unique rows for Emp_id,Emp_name,Emp_email like ,
Emp_id,Emp_name,Emp_email
1 abc a@b.com
2 abc a@b.com| Report Duplicate | Flag | PURGE
Ebay Member Technical Staff SQL - 0of 0 votes
AnswersHow Denormalization improves performance ?
- vasa.v03 in United States| Report Duplicate | Flag | PURGE
Ebay Member Technical Staff SQL - -1of 1 vote
AnswersRead a file and create a a datastructure which holds all the anagrams of words conatined in the file..
- vasa.v03 in India
For e.g lets say file content is "abc bca"
we need a DS to say "abc" and "cba" are anagrams.
I told i will use a FileReader to read characters than bytes
and assing a prime number for each alphabet
say
a - 2
b - 3
c - 5
and calculate the compound ofr multiplication
say abc = 2 * 3 * 5 = 30
bca = 3 * 5 * 2 = 30
i will use the compound a key in hashmap.
like 30 = abc-> bca -> cba
Let me know for any other better solution| Report Duplicate | Flag | PURGE
Ebay Member Technical Staff Data Structures
Hash each string using some mechanism
Assuming we are dealing only with alphabets and strings are not too long
as hash might exceed data type limits.
i have assigned 2 pow for each alphabet.
so i calc hash of both string with O(n) and compare the hashes.
public static boolean IsAnagram(String s1, String s2) {
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
if(c1.length != c2.length)
{
System.out.println("Length mismatch .. Not Anagrams");
return false;
}
else
{
int s1_hash = 0,
s2_hash = 0;
for (int i=0; i< c1.length ; i++ )
{
s1_hash += hash(c1[i]);
s2_hash += hash(c2[i]);
}
if (s1_hash == s2_hash )
{
System.out.println("!! Anagrams.... !!");
return true;
}
else
{
System.out.println("Hash mismatch .. Not Anagrams");
return false;
}
}
}
static int hash(char c)
{
switch (c)
{
case 'a' :
return 1;
case 'b' :
return 2;
case 'c' :
return 4;
case 'd' :
return 8;
case 'e' :
return 16;
case 'f' :
return 32;
case 'g' :
return 64;
case 'h' :
return 128;
case 'i' :
return 256;
case 'j' :
return 512;
case 'k' :
return 1024;
case 'l' :
return 2048;
case 'm' :
return 4096;
case 'n' :
return 8192;
case 'o' :
return 16384;
case 'p' :
return 32768;
case 'q' :
return 65536;
case 'r' :
return 131072;
case 's' :
return 262144;
case 't' :
return 524288;
case 'u' :
return 1048576;
case 'v' :
return 2097152;
case 'w' :
return 4194304;
case 'x' :
return 8388608;
case 'y' :
return 16777216;
case 'z' :
return 33554432;
default:
return 0;
}
}
To generalize the solution (assuming we have a balance) ,
lets say there are N balls ,
in any case we will split it up into 3 sets
First and Second set will have ceil(N/3) no of balls
and third set will have N - 2( ceil(N/3) ) no of balls
S(1) = S(2) = abs(N/3)
S(3) = N - 2( abs(N/3) )
And number of times we weigh in balance should be logN with log base as 3
n = logN
3 ^ n = N
BlockingQueue which holds the connection objects with Block size equal to connection pool size.
So at intialization, connection objects will be created and queued in ArrayBlockingQueue .
For any request for connection pop an connection object from BlockingQueue.
if the queue is empty the request will wait and close method of Connection object to be overriden to offer the connection back to blocking queue instead of closing the connection.
Same as @eugene.yarovoi told except the need of declaring the inner class as
static
is that the inner class wont be defined everytime an intance of outer class is created.
Once we declare the inner class as static the innder class is defined and loaded once when outer class is loaded.
Basically the solution calls for a BiDirectional Map or Bidi Map
I saw this code on stackoverflow from @GETah
public class BidirectionalMap<KeyType, ValueType>{
private Map<KeyType, ValueType> keyToValueMap = new ConcurrentHashMap<KeyType, ValueType>();
private Map<ValueType, KeyType> valueToKeyMap = new ConcurrentHashMap<ValueType, KeyType>();
synchronized public void put(KeyType key, ValueType value){
keyToValueMap.put(key, value);
valueToKeyMap.put(value, key);
}
synchronized public ValueType removeByKey(KeyType key){
ValueType removedValue = keyToValueMap.remove(key);
valueToKeyMap.remove(removedValue);
return removedValue;
}
synchronized public KeyType removeByValue(ValueType value){
KeyType removedKey = valueToKeyMap.remove(value);
keyToValueMap.remove(removedKey);
return removedKey;
}
public boolean containsKey(KeyType key){
return keyToValueMap.containsKey(key);
}
public boolean containsValue(ValueType value){
return keyToValueMap.containsValue(value);
}
public KeyType getKey(ValueType value){
return valueToKeyMap.get(value);
}
public ValueType get(KeyType key){
return keyToValueMap.get(key);
}
}
public static long exp(int a , int n)
{
boolean is_N_Odd = false;
if ( n % 2 > 0 )
{
n++;
is_N_Odd =true;
}
if ( n == 2 )
return a * a;
else if (n == 1 )
return a;
else
{
long calc = exp (a , n/2) ;
if ( is_N_Odd )
return calc * calc /a;
else
return calc * calc ;
}
}
since weigh balance is used we should consider splitting the group into 3 not 2 . 12 marbles should be split into three groups 4(g1) ,4(g2) , 4(g3) and weighed in the balance a below.
- vasa.v03 December 28, 2015Assume g3 contains a heavier marble:
Case : (compare g1 and g2) If the g1 and g2 are weighed the balance will remain same so discard g1 , g2 and continue the same process with g3
Case where g1 is compared to g3 : weight balance will lean towards g3 heavier side so discard g1, g2 and and continue the same process with g3.
so Complexity becomes O(logN) where N is number of marbles and base of log is 3 not 2.