Ashutosh
BAN USER- 0of 0 votes
AnswersGiven a large buffer which contains almost infinite number of words. You are given an API "char* getNextWord(int* len)". You need to find all the words & their frequency in the buffer.
- Ashutosh in United States
For eg. char* buff = { "I", "AM", "Programmer", "AM", "I", "Boy" ....}.. then
Output = I = 2, AM =2, Programmer =1, boy =1.| Report Duplicate | Flag | PURGE
Software Engineer / Developer
Here's my solution
#include <iostream>
using namespace std;
int unmberoftrails(unsigned int numberofcoins)
{
if(numberofcoins)
{
if(numberofcoins>3)
{
if(numberofcoins%3) // not divisible by 3
{
if(numberofcoins%2) // not divisible by 2
{
return 1 + unmberoftrails(numberofcoins-1);
}
else //divisible by 2
{
return 1 + unmberoftrails(numberofcoins/2);
}
}
else //divisible by 3
{
return 1 + unmberoftrails(numberofcoins/3);
}
}
else // 1 ,2 ,3 shall take only 1
{
return 1;
}
}
return 0;
}
int main()
{
unsigned int noc = 0;
cout << "\n Entern number of coins => ";cin>>noc;
cout<<"\n Number of trails needed = "<<unmberoftrails(noc)<<"\n";
return 0;
}
Please let me know if there is a problem!!!
- Ashutosh November 13, 2012
Hi,
- Ashutosh September 20, 2015for first part
1. While reading you do not need the lock
2. While updating you need lock.
so ,
UpdateAPI (int num){
lock;
if(max<num){ max=num;}
if(min>mum){ min=num;}
unlock;
}
}
PrintAPI(){
cout<<max<<min;
}
Some might argue, at the time of update being called in some other thread goes for print then the data is inconsistent,
indeed it that way. This code is written with that intention. If consistency is important then, we got to do the lock / unlock in the print code as well.
PrintAPI(){
lock;
cout<<max<<min;
unlock;
}
second part is just load balancing.
Third part is interesting. we need 10 numbers now, hence one variable will not do
we need have an enlarge data structure