Knewton Interview Question for Computer Scientists


Team: Big Data
Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
1
of 1 vote

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'

For 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

- babbupandey August 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

For the 2nd part, if you sort the string at first with Quick Sort or some smart sort algorithms. Simply, you need one temp value to check duplicates.
That makes time complex O(NlogN) + O(N)

- nsdxnsk August 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Actually, the second part depends a lot on what you answered for part a. I think the best solution so far is the bitmap one as described by babbupandey.

- chandershivdasani August 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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 :)

- amustea August 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Length of string is less than 20 chars.

- babbupandey August 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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)

- satya August 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Abhishek August 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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!!!

- KKR August 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

sort the array & take two char variable previous & current if at any time prev =current return as repeated element....Time complexity O(nlogn) Space constant

- atul gupta August 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

- basavaraja.b August 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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;
}

- Basavaraj B August 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Won't using an array indexed by a char-value modulo string-length run the risk of collisions?

- Thomas August 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well that is the intention behind it. Collision would let us know if the string contains

- Basavaraj B August 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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..

- Basavaraj B August 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Of course. I mean, there is a chance that two different characters will map to the same value, making collisions ambiguous.

- Thomas August 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are right. It should better be 256 only.

- Basavaraj B August 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sort using merge sort with complexity O(nlogn) and then traverse the string by comparing adjacent chars if they equal string contains at least one char repeated..

- Gowda August 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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]++;

- yash August 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you can use an array of size 256 (for ASCII)like int arr[256]

int arr[256],a;
for(int i=0;i<strlen(str);i++)
{
a=str[i];
if(arr[a]!=0)
arr[a]++;//if that character is appearing for first time
else
return 0;//already seen character
}

- Nik August 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

correction:
replace if(arr[a]!=0) with if(arr[a]==0)

- Rao N August 22, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More