mohapatrasandeep60
BAN USER
- 1of 1 vote
AnswersThere are 3 threads. 1 is producer thread. 2 consumer threads. There is 1 buffer. Producer writes to the buffer. Consumers consume from buffer. How to synchronise between them so that consumers consume from buffer one after another, that is, the number of tokens that consumer thread1 consumes is same as number of tokens consumer thread2 consumes. Answer in c,linux
- mohapatrasandeep60 in India| Report Duplicate | Flag | PURGE
Hewlett Packard SDE-2 - 0of 0 votes
AnswersI read this question here in careercup and saw no comments on it. So, I am answering it.
- mohapatrasandeep60 in United States
given a string find the sum of number of unique substring characters.
means if string is ACAX. the sub strings are A,C,A,X,AC,CA,AX,ACA,CAX,ACAX. in ACA, A occurs more than once so, its count is not considered, but C occurs once so its count is 1. Similarly in ACAX, A's count=0,C's count=1,X's count=1. In such a manner find sum of all counts for all substrings. The worst case time complexityis O(n). IF same substring occurs more than once then find its sum for that many times.| Report Duplicate | Flag | PURGE
Amazon - 0of 0 votes
Answersgiven an array of integers suppose 1234, print all groups of integer array possible of length upto n where n can be any number greater than zero
- mohapatrasandeep60 in India
example for n=5
1,11,111,1111,11111,12341,22222,2222,222,22,2 etc
for n=3
1,11,123 etc| Report Duplicate | Flag | PURGE
Samsung Developer Program Engineer - 1of 1 vote
AnswersHow to develop a web based multiplayer chess game. There will be 2 player in each session. There will be multiple sessions.
- mohapatrasandeep60 in India for no
My answer(I was not selected)
Each player will have a database with columns
user id(primary key), passwd,no of games played, won,drawn,last active time,sq1,sq2,...sq64,move made flag,pawn num, sqstart,sqend
where sq1-denotes square 1 and its value represents the pawn num in sq1
question- what all fields in database will be encrypted
answer-passwd
question- why not others
answer-may be system admin needs to access them
question – how will u start a session between players?
Answer-When a player logs in, his last active time is updated. Each player is shown a list of last active players to chose from.Or system can arbitrarily select one.When a player is selected, he gets a request. Each player receives a list of requests from other players. He choses from one. That player gets an acknowledgement others get negative acknowledgement. If acknowledgement does not come in time then that player is dropped and another player is selected.
Question- http connection, can not send request from server. How the 2 players will send request to each other and how session will be initiated?
Question-how the game will be stored?
The 64 squares sq1,sq2,..sq64 will each store the pawn number in them. From random number calculatio, it will be decided which one is white and which one is black 1st time. When a player makes move,has made move flag will be true. The last move is stored as sqstart,sqend,pawn number. Each time a move is made check for valid move.The other player continuously polls for has made move flag to know if it is his turn
remarks- ur design is not scalable.| Report Duplicate | Flag | PURGE
Amazon SDE-2 - 0of 0 votes
Answersif you have in a file
- mohapatrasandeep60 in India
int fn(int,int,int);
float fn(int,int);
it compiles fine
but if you have
int fn(int,int);
float fn(int,int);
it gives compilation error
so, is the following an example of function overloading?
int fn(int,int,int);
int fn(int,int);| Report Duplicate | Flag | PURGE
Harman Kadron - 0of 0 votes
Answersin linux threads are called light weight processes. Whether process or thread, they are implemented by task_struct data structure. 1>So, in that sense how kernel distinguishes between thread and process.
- mohapatrasandeep60 in India
2> when context switching happens, how threads get less overhead in context switching. because prior to this thread, another thread from another process may be running. So kernel should load all resources even if resources are shared between threads of a processes| Report Duplicate | Flag | PURGE
TATA Consultancy Services - 0of 0 votes
Answerswhat happens if time of pthread_cond_timedwait expires?
- mohapatrasandeep60 in India| Report Duplicate | Flag | PURGE
TATA Consultancy Services - 0of 0 votes
Answerswe create a pipe
- mohapatrasandeep60 in United States
int pfd[2];
if (pipe(pfd) == -1) /* Create the pipe */
errExit("pipe");
what will happen if we write to or read from both pfd[0], pfd[1] simultaneously
how to handle such a situation| Report Duplicate | Flag | PURGE
TATA Consultancy Services Tech Lead - 0of 0 votes
Answersis there anything wrong in the code
- mohapatrasandeep60 in India
class B
{
~B(){}
public:
void Destroy()
{
delete this;
}
};
int main() {
B* b = new B();
b->Destroy();
return 0;
}
why you cant create object of class b on stack| Report Duplicate | Flag | PURGE
TATA Consultancy Services - 0of 0 votes
Answerwhat is the use of dynamic_cast in c++
- mohapatrasandeep60 in India| Report Duplicate | Flag | PURGE
HCL - 0of 0 votes
AnswersWhy to use singleton class? What is the advantage you get by restricting single object of a class.
- mohapatrasandeep60 in India
eg.If you are using a logger class, if there are many objects of the class and you want to print that sequentially, you can achieve this with mutex. Why use singleton?
write code for thread safe singleton.| Report Duplicate | Flag | PURGE
HCL Developer Program Engineer - 0of 0 votes
Answerswrite commands for -
- mohapatrasandeep60 in India
1>list processes
2>remove 2lines from file
3>there are multiple hirarachical direcories. Change the file extension all .c files to .cpp
4>chang time stamp of a file
5> run a script at a particular time| Report Duplicate | Flag | PURGE
HCL Developer Program Engineer - 0of 0 votes
Answersthere is a chess board of dimension n X n. You are given with 2 squares on that board S(x1,y1) ;M(x2,y2). S is a fixed point. M can move diagonally. it can move any number of steps or jumps in 1 move . Find the minimum number of moves M needs to reach S
- mohapatrasandeep60 in India| Report Duplicate | Flag | PURGE
Amazon SDE-2 Coding - 1of 1 vote
Answersthere is a news publishing/subscribing product of Amazon where electronic contents are collected from owners like newspaper, magazines. Customer is using kindle. Design how customer will get the content when his system kindle connects to net. how to send the contents to device?
- mohapatrasandeep60 in India| Report Duplicate | Flag | PURGE
Amazon SDE-2 Software Design
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct node
{
struct node*left;
struct node*right;
int key;
static int maxheight;
};
int node::maxheight =0;
struct node * insert(struct node*root,int key,int height=0)
{
if(root == NULL)
{
if(height>node::maxheight) node::maxheight=height;
struct node * temp=new node;
temp->key = key;
temp->left=temp->right=NULL;
return temp;
}
if(key > root->key)
{
root->right = insert(root->right,key,height+1);
}
else if(key < root->key)
{
root->left = insert(root->left,key,height+1);
}
}
void printTree(struct node*root, int level=0, int maxheight=node::maxheight)
{
if(root==NULL) return;
printTree(root->left,level+1);
for(int i=maxheight-level;i>=0;i--) cout<<" ";
cout<<root->key<<endl;
if(root->key<10) cout<<" "<<endl;
printTree(root->right,level+1);
/*for(int i=maxheight-level;i>=0;i--) cout<<" ";
cout<<root->key<<endl;
if(root->key<10) cout<<" "<<endl; */
}
struct node * best_fit_memory(struct node* root,int input_block_size,struct node* best_fit=0)
{
if(root)
{
if(root->key > input_block_size && ((best_fit && best_fit->key > root->key)||!best_fit))
best_fit = root;
}
else
{
return best_fit;
}
if(input_block_size > root->key)
{
best_fit = best_fit_memory(root->right, input_block_size,best_fit);
}
else if(input_block_size < root->key)
{
best_fit = best_fit_memory(root->left, input_block_size,best_fit);
}
else
{
best_fit = root;
//return root;
}
return best_fit;
}
int main()
{
struct node*root=NULL;
root = insert(root,8);
printTree(root);
cout<<"------------------------------------------------------------------------------"<<endl;
root = insert(root,7);
printTree(root);
cout<<"------------------------------------------------------------------------------"<<endl;
root = insert(root,10);
printTree(root);
cout<<"------------------------------------------------------------------------------"<<endl;
root = insert(root,9);
printTree(root);
cout<<"------------------------------------------------------------------------------"<<endl;
root = insert(root,5);
printTree(root);
cout<<"------------------------------------------------------------------------------"<<endl;
struct node* result_best_fit = best_fit_memory(root,6);
cout<<"for input "<<6<<"resultbest fit = "<<result_best_fit->key<<endl;
}
posting code with comments
#include <iostream>
#include <stdlib.h>
using namespace std;
int solution(char* str)
{
//array to hold count of occurance of 26 alphabets a-z
int characterCount[26]={0};
int* characterPositionList[26]={0};
int characterPositionListIndex[26]={0};
int sum=0,left=0,right=0,len=0,flag=0;
//loop from begining of str to end of str
for(;str[len]!='\0';len++)
{
//str[len]-'A' converts the alphabet to int representation
//example->if str[len] is 'A', 'A'-'A' = 0 which is the 1st index of characterCount[]
//if str[len] is 'Z', 'Z'-'A' = 25 which is the 26th index of characterCount[]
characterCount[str[len]-'A']++;
}
cout<<"len="<<len<<endl;
//loop from begining of characterCount[] to end
for(int i=0;i<26;i++)
{
//if characterCount[i] is not zero, means alphabet at position has count greater than zero
if(characterCount[i])
{
//create characterPositionList[i][j], where each individual element of characterPositionList[i]is a pointer to integer array
//in ith position of characterPositionList,create another integer array
//where i indicates the alphabet like in characterCount[i]
//where j indicates the next occurrence of the same alphabet so that characterPositionList[i][j] is the position of the next occurence of alphbet in input array str[]
//example in "ACAX", there are 2 occurences of A at str[0] and str[2] and total number of occurences of 'A'=2
//so characterPositionList[0] points to array of length 2, where characterPositionList[0][0]=0 and characterPositionList[0][1]=2
//the new array that characterPositionList[i] points to is populated in next for loop
characterPositionList[i] = new int[characterCount[i]];
}
}
for(int i=0;i<len;i++)
{
//the new array that characterPositionList[i] points to is populated here
//characterPositionListIndex[i] points to last element j of characterPositionList[i][j] for a given i
characterPositionList[str[i]-'A'][characterPositionListIndex[str[i]-'A']++] = i;
}
for(int i=0;i<26;i++)
{
if(characterPositionList[i])
{
//consider "ACAX" and character 'C'(ie, characterPositionList[1][0]),so the character starts at position1,ie,characterPositionList[2][0]=1, call it k.
//So it can form k+1 substrings that we can consider,ie, "AC" and "C", takinginto countfrom begining till this character
//but we consider 1-character substrings atthe end separately with the statement "sum+=characterCount[i]"(there can be characterCount[i] 1-character substring)
//so, we exclude 1-character-substring and consider k only.
sum+= characterPositionList[i][0]-0;
int j=1;
for(;j<characterCount[i];j++)
{
//here consider thecharacter 'A'. It has 2 positions in "ACAX",at 0 and 2.
//we calculate the number of characters in between excluding 'A's.There is only 1 'C'.
//the number of substrings itcan form with left-side 'A'is 1,ie,"CA" and with right is 1,ie,"AC". So, the result is multiplied by 2 forleft-side calculation andright-side calculation
sum+= ((characterPositionList[i][j]-characterPositionList[i][j-1]-1)*2);
}
//Simmilar to "sum+= characterPositionList[i][0]-0", it considers the numberofsub-strings thatcan be formed from lastoccurence ofan alphabet to end of string
sum+= (len - characterPositionList[i][j-1]- 1);
//sum++;
//till now we have consideredsub-strings with the given alphabet at the end, now we consider with given alphabet in middle
//consider string "ACAXC" and character 'C'
//left = characterPositionList[1][0]-0=1. So, there is 1 character to the left of 'C'
left = characterPositionList[i][0]-0;
if(characterCount[i]>1)
{
//right=characterPositionList[1][1]-characterPositionList[1][0]=4-1=3
//so,there are 3 characters in right
right = characterPositionList[i][1]-characterPositionList[i][0]-1;
//total permutations ofcombiningthese leftand rightcharacters to form sub-strings=(left*right)
sum+= (left*right);
for(j=2;j<characterCount[i];j++)
{
left = characterPositionList[i][j-1]-characterPositionList[i][j-2]-1;
right = characterPositionList[i][j]-characterPositionList[i][j-1]-1;
sum+= (left*right);
}
left = characterPositionList[i][j-1]-characterPositionList[i][j-2]-1;
}
right = len-characterPositionList[i][j-1]-1;
sum+= (left*right);
sum+= characterCount[i];
}
}
return sum;
}
int main()
{
char str[]="ACAX";
cout<<solution(str)<<endl;
}
#include <iostream>
#include <stdlib.h>
using namespace std;
int solution(char* str)
{
int characterCount[26]={0};
int* characterPositionList[26]={0};
int characterPositionListIndex[26]={0};
int sum=0,left=0,right=0,len=0,flag=0;
for(;str[len]!='\0';len++)
{
characterCount[str[len]-'A']++;
}
cout<<"len="<<len<<endl;
for(int i=0;i<26;i++)
{
if(characterCount[i])
{
characterPositionList[i] = new int[characterCount[i]];
}
}
for(int i=0;i<len;i++)
{
characterPositionList[str[i]-'A'][characterPositionListIndex[str[i]-'A']++] = i;
}
for(int i=0;i<26;i++)
{
if(characterPositionList[i])
{
sum+= characterPositionList[i][0]-0;
int j=1;
for(;j<characterCount[i];j++)
{
sum+= ((characterPositionList[i][j]-characterPositionList[i][j-1]-1)*2);
}
sum+= (len - characterPositionList[i][j-1]- 1);
//sum++;
left = characterPositionList[i][0]-0;
if(characterCount[i]>1)
{
right = characterPositionList[i][1]-characterPositionList[i][0];
sum+= (left*right);
for(j=2;j<characterCount[i];j++)
{
left = characterPositionList[i][j-1]-characterPositionList[i][j-2]-1;
right = characterPositionList[i][j]-characterPositionList[i][j-1]-1;
sum+= (left*right);
}
left = characterPositionList[i][j-1]-characterPositionList[i][j-2]-1;
}
right = len-characterPositionList[i][j-1]-1;
sum+= (left*right);
sum+= characterCount[i];
}
}
return sum;
}
int main()
{
char str[]="ACAX";
cout<<solution(str)<<endl;
}
can somebody give me a link to read about such web based designs
- mohapatrasandeep60 September 11, 2017int longest_streak(bool a[],int k)
{
int size = sizeof(a)/sizeof(bool);
vector<int> countOfOneZero;
int count = 0;
for(int i = 0; i< size; )
{
while(a[i++]) count++;// this is count of consecutive 1
countOfOneZero.push_back(count);
count = 0;
while(!a[i++]) count++;//this is count of consecutive 0
countOfOneZero.push_back(count);
count = 0;
}
if(a[i-1]) countOfOneZero.push_back(0);// if the given array ends with 1, then count of //trailing 0 is 0
//at the end of this loop countOfOneZero contains count of consecutive 1 followed by //count of consecutive 0 .... and even indexes give count of 1 and odd index gives count //of zero
int currentStreak(0), maxStreak(0),availableFlipCount(k);
for(vector<int>::iterator it = countOfOneZero.begin(); (it+1) != countOfOneZero.end(); it+=2)
{
currentStreak = 0;
availableFlipCount = k;
for(vector<int>::iterator it2 = it; (it2+1) != countOfOneZero.end(); it2+=2)
{
currentStreak+=*it2;
if(availableFlipCount > *(it2+1))
{
currentStreak+=*(it2+1);
availableFlipCount -= *(it2+1);
if(!availableFlipCount)
{
if((it2+1) != countOfOneZero.end())
{
currentStreak+=*(it2+2);
if(currentStreak > maxStreak) maxStreak = currentStreak;
it2 = countOfOneZero.end() -1;
}
}
}
else
{
currentStreak+=availableFlipCount;
availableFlipCount = 0;
if(currentStreak > maxStreak) maxStreak = currentStreak;
it2 = countOfOneZero.end() -1;
}
}
}
}
modify the above code
before the code "for(vector<array>::iterator it2= result.begin();it2!=result.end();it2++)"
calculate size of result into variable z.
make the loop run until z number of elements
typedef vector<int> array
vector<array> findNumberForSum(vector<int> arr, int sum)
{
vector<array> result;
vector<int> x;
x.push_back(0);
result.push_back(x);
for(vector<int>::iterator it= arr.begin();it!=arr.end();it++)
{
int len = result.size();
for(int it2=0; it2<len;it2++)
{
if(result[it2][result[it2].size()-1]+*it <=sum)
{
vector<int> temp(result[it2]);//this array contains elements that add less than or
//equal to sum followed by sum of integers present in
// the array
temp.push_back( result[it2][result[it2].size()-1] + *it);//insert new sum
temp[temp.siz()-2] = *it;
}
}
}
for(int i-0;i<result.size();i++)
{
if(result[i][result[i].size()-1] != sum)
{
result.erase(result.begin()+i);
}
}
return result;
}
}
- mohapatrasandeep60 February 01, 2016char findNonRepeated(string str)
{
int count[27]={0,0,0,0,0,0,0,0,0,0,0....27 times};
char find;
int temp;
for(string::reverse_iterator i=str.rbegin();i!=str.rend(),i++)
{
temp = *i-'a';
if (!(temp>=0 && temp<=25 ))
{
temp = *i-'A';
if (!(temp>=0 && temp<=25 ))
temp=26;//blanc space
}
.if(a[temp]++==0)
find= *it;
}
return find;
}
loop through the string from back
make an array a[26], where a[0]= count of 'a' and a[25]=count of 'z'
make a temp(find) char variable
if the char is in a[], increment count
if the char is not in a[](a[i]==0), increment count, put it into find
after the end of loop, the value of find is the 1st non-repeated char
Repryandchinkle, Android Engineer at ABC TECH SUPPORT
I was born in Northridge USA, I like photography, I have wide photos collection of wildlife. I have many religious ...
RepTaylahMacge, Consultant at Accenture
Assigned to manage the requirements of foreign clients specifically those from the Chinese, Arabic, and French-speaking markets.I am passionate ...
RepShastri Ji is well known hindi and tamil vashikaran specialist. He will give you effective and simple totke to control ...
RepAlmaRJude, Quality Assurance Engineer at Brocade
I am Alma from Livermore USA, I also enjoy reading – my favourite topics being social history, the development and use ...
RepHi, I am Alisa from Southfield, USA, I am working as a Toolmaker in an Audio-Visions company. I am also ...
Repcardiroy, Backend Developer at Accolite software
Hi,I am from Taxes, USA. Enthusiastic multilingual translator with years involvement with Spanish-English translations.Looking to additionally improve interpretation ...
Repwilliamland1990, Associate at Advent
Gifted in donating shaving cream worldwide. Crossed the country getting my feet wet with the elderly in Salisbury, MD. Have ...
RepAvikaEthan, Data Engineer at Adjetter Media Network Pvt Ltd.
Avika Ethan has five years of experience collecting physical and biological data in California streams and rivers. In the field ...
//remove duplicates from result
- mohapatrasandeep60 June 18, 2018