thePlatform Interview Question for Software Engineer in Tests


Country: United States
Interview Type: Phone Interview




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

Using hash is not allowed whether your write your own hash function or you use the one available in the java collections.
Sorting seems fine but it gives you O(n* log n) complexity.
A better means would be to declare a 256-bit BitSet. The index of each of the slots of this bit array would represent the Ascii code.
Now we just traverse through the given string and change the bit value in the BitSet(givenString.charAt(i)) from 0 to 1.
If we confront a 1 instead of a 0 for a character then we return false.
If we successfully traverse the whole string it implies the string has all unique characters.
O(n) would be the time complexity for this.

- teli.vaibhav February 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The issue with this is they'd specified any sort of character, you could assume that means UNICODE and then just expand the size of your array but that might not suffice if they want to be extremely pedantic.
You could add in another O(n) operation to detect the highest valued character contained in your string and then allocate your array that size, even if we start using a different character set in the future you're covered.

- Mike Nargang February 14, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Is sort permitted? If so, you can sort the string a first, and use this code to check duplicate chars:

public boolean hasUniqueChars(String a){
  for(int i=0; i< a.lenghth(); i++){
    if (a.charAt(i) == a.charAt(i+1)
       return false;
  }
  return true;
}

- RW February 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yes I did it by sorting as well !!

- HB February 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Sorting uses minimal of O(nlogn) Time Complexity.

Better use the hash to validate the uniqueness. its O(n) algo

- hprem991 February 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

not allowed to use any collections... how are you planning to use hash??

- HB February 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Seems like this guy always does that. I have noticed it before.

- Anonymous February 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can write a Small Hash by your own. its 5 line program and the array to hold the value for facilitation. You don't need to use any collection. Its your own Data Structure

- hprem991 February 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

then it sounds good !!!

- HB February 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@HB: The collections are not magic. It was implemented someway. You could do that yourself too and say you are not relying on it.

- Anonymous February 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anonymous: yep, it dint click me to implement hash at that time.. well.. I did that using merge sort.. anyways..

- HB February 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use an array of size 256 (assuming char is 1 byte). Use that as the hash table...

- Anonymous February 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nope.. its any char set.. consider Unicode.. basically count array approach not feasible :(

- HB February 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorting the string is the best option.
If changing the string not allowed, then u will have to store the sorted array using extra memory.

- Anonymous February 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isUniqueChars2(String str) {
 boolean[] char_set = new boolean[256];
 for (int i = 0; i < str.length(); i++) {
 int val = str.charAt(i);
 if (char_set[val]) return false;
 char_set[val] = true;
}
}

- sree.sdet February 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

hasUniqueChars not isUniqueChars

- Anonymous February 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean hasUniqueChars(String s) {
		int[] char_set = new int[256];
		for(int i=0; i<s.length(); i++) {
			int index = s.charAt(i);
			char_set[index]++;
		}
		for(int i=0; i<char_set.length; i++) {
			if(char_set[i]>1) return false;
		}
		return true;

}

- Anonymous April 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

consider the unicode, I fix it:

public static boolean hasUniqueChars(String s) {
		int[] char_set = new int[65535];
		for(int i=0; i<s.length(); i++) {
			int index = s.charAt(i);
			char_set[index]++;
		}
		for(int i=0; i<char_set.length; i++) {
			if(char_set[i]>1) {
				return false;
			}
		}
		return true;

}

- Xiang Li April 22, 2013 | 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