alex
BAN USER- 0of 0 votes
AnswersRemove duplicates from a string inplace. The algorithm should be as efficient as possible.
- alex in India
I gave two approaches. First, the simple comparison O(n2) and second, sorting O(nlgon). But the interviewer did not seem satisfied.
Can someone please suggest a better algorithm?| Report Duplicate | Flag | PURGE
Microsoft Intern Algorithm
#include<iostream>
using namespace std;
int main(){
char c[100];
int d;
cout<<"Enter the string";
for(int i=0;i<100;i++){
cin>>c[i];
d=i;
if(c[i]=='/n')
break;
}
for(int d;d>=0;d--;)
cout<<c[d];
}
I am not sure if I am getting the ques right. I have assumed that you keep on reading till an enter key is pressed and then you print the reverse string. If you want to store the reversed string and then print, maintain two pointers, one at beginning and the other at last and keep swapping. If I am wrong, please explain the ques properly.
As we copy a node, we insert it into the table. If we later find that one of a node’s neighbor is already in the table, we do not make a copy of that neighbor, but to push its neighbor’s copy to its copy instead. Therefore, the hash table would need to store a mapping of key-value pairs, where the key is a node in the original graph and its value is the node’s copy.
- alex March 07, 2013i am assuming that the number is in the form of digits..if not, then split it into digits and do this:
count=1,j=1;
for(int i=0;i<10;i++){
while(arr[j]){
if(arr[j]==arr[j-1]){
count++;
}
else{
cout<<count;
cout<<arr[j-1];
count=1;
}
j++;
}
cout<<count;
cout<<arr[j];
}
Please let me know if something is wrong.
If the number,n, is an exact power of two, return n-1
else find the nearest power of 2 to the number, find their difference,d, and return d-1
also, if n=0 return 1
ex: n=14
nearest power=16
d=16-14=2
return d-1=1
if n=16, since it is an exact power of 2, return 16-1=15
Which ball do you call faulty?
- alex April 06, 2013