Knewton Interview Question
Computer ScientistsTeam: Big Data
Country: United States
Interview Type: Phone Interview
You can use a hash to store each char. If a char is already in the hash, it contains a duplicate.
If you are allowed to modify the string you can sort it and then search for two adjacent characters being the same char.
For part B - what do you mean by 20 chars? Length of string is not greater than 20 or there are 20 different letters in the string? You can use an array of size 20 or an int (right-most 20 bits of it to be more precise).
Code to follow :)
I think if we keep arranging the ASCII values in Binary Search Tree as we parse the string. So if the ASCII values matches with any node than its a duplicate or else we insert the ASCII value in BST. This will take maximum time as O(height of tree)
It is a better method of reducing the complexity from O(n^2) to O(height of the tree).
I think an creating the binary search tree is equivalent to having an hash as far as restraint on extra memory is concerned
Building Binary search tree is a good approach...
The only thing is creating the binary search tree will take O(n.logn) time...which is better option as compared to sorting and then searching.....
But i still think hasmap implementation is a good approach large number of elements, given we can use extra memory to maintain hashmap in memory..
but for 20 elements if we have a constraint of memory sorting will be the best option elements..which will take O(20 log 20) for sorting and traversing 20 elements in worst case to check the neighboring elements in an array..
Please comment if you guys disagree!!!
bool find_dup(char *s)
{
int len = strlen(s);
bool hash[256] = {false};
for (int i =0; i<len; i++)
{
if (!hash[s[i]])
hash[s[i]] = true;
else
return true;
}
return false;
}
Improved one (Memory saver):
bool find_dup(char *s, int len)
{
bool hash[len] = {false};
for (int i =0; i<len; i++)
{
if (!hash[s[i]%len])
hash[s[i]%len] = true;
else
return true;
}
return false;
}
Won't using an array indexed by a char-value modulo string-length run the risk of collisions?
Well that is the intention behind it. Collision would let us know if the string contains
Well that is the intention behind this. Collision would let us know if the string contains repeated character or not! If a collision occurs, yes that particular character is repeated one..
Of course. I mean, there is a chance that two different characters will map to the same value, making collisions ambiguous.
We can reduce the array size to 52 if the string strictly contains only alphabets,
array[52];
eg: if the string is 'abCd'
take a variable as int like int k;
then k=string[0]-'a';
then array[k]++;
if array[k] is already more than zero then it means the character k is being repeated,
for C
k= string[2]-'A'+26;
then array[k]++;
I assumed all characters are ASCII, and then I created a bool array of 200, reading one character at a time and then marking the corresponding ASCII (numeric) value as true every time I encounter a character. When I see a character and its value in bool array has already been set to true, then I returned 'duplicate' otherwise 'correct'
- babbupandey August 22, 2012For the second part, I said that since we know the upper limit of string length, we can follow the O(n^2) approach (check each character by all other characters). We will be repeating the loop at most 400 times (20x20). The interviewer, I guess, was not impressed