Interview Question


Country: United States
Interview Type: In-Person




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

public static boolean isUniqueChars(String str) {
int checker = 0;
for (int i = 0; i < str.length(); ++i) {
int val = str.charAt(i) - 'a';
if ((checker & (1 << val)) > 0)
return false;
checker |= (1 << val);
}
return true;
}

- limca500ml January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you explain it.I did it for other uniqueness of element in an array.
But I forget concept .Please tell me

- Raj January 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

int checker = 0;
// readt each char one by one
for (int i = 0; i < str.length(); ++i) {
// get unique ASCII value for current char
int val = str.charAt(i) - 'a';
// if bit is not on for that "val"th place
if ((checker & (1 << val)) > 0)
return false;
// Turn on the bit at val place and or to checker
checker |= (1 << val);
}
return true;

// Checker is a bit map. We have one to one mapping between "n"th bit and "n"th char
// Just turning on that bit if char present .
Please let me know if it still unclear

- limca500ml January 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In 7 byte ascii there could be 128 different characters hence you would require 128 bits. I guess using a simple it would result in overflow.

- JayDee January 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi
i am unclear with the statement checker & (1 << val)) > 0
can you please give little more exolaination..

- Shrikant January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void findUniqueCharactersInString() {
		char[] s = "Write a program to find the unique char in the ascii string.".toCharArray();
		int i = 0;
		boolean[] chars = new boolean[512];
		while (i < s.length) {
			int j = i + 1;
			while (j < s.length && !chars[s[i]]) {
				if (s[i] == s[j]) {
					break;
				}
				j++;
			}
			if (j == s.length) {
				System.out.println(s[i] + " ");
			}
			chars[s[i]] = true;
			i++;
		}

}

- Rocko January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple forloop in Python

#Write a program to find the unique char in the ascii string.
string = 'Federico'
uniques = []
for letter in string:
	if letter not in uniques:
		uniques.append( letter)

#output = Fedrico
print ''.join( uniques )

- Federico G January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As the string is ASCII we can easily use a bitmap to count the occurrence of each character. But as the question asks for uniqueness, counting is not required and one bit per element should be sufficient. Anyways here is the code for general count based bitmap solution

long[] bitmap = new long[256];
	
	public void printUniqChar(char[] s) {
		for(char c:s)
			bitmap[c]++;
		for(int i=0; i<bitmap.length; i++)
			if(bitmap[i] == 1)
				System.out.printf("UNIQUE = %c\n", i);
	}

- JayDee January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include <map>

using namespace std;


int main(){
char arr[]="hello";

map<char,int> mymap;
map<char,int>::iterator it;
int i=0;
while(arr[i]){
if(!mymap.count(arr[i]))
mymap[arr[i]]=0;
else
mymap[arr[i]]=1;
i++;
}

for (it=mymap.begin(); it != mymap.end(); it++ )
cout << (*it).first << " => " << (*it).second << endl;


for(int i=0;i<10;i++){
if(!mymap.find(arr[i])->second)
cout<<"unique first number is:"<<arr[i]<<endl;
//break;
}

}

- Raj January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I've been working on the algorithm and here's what I noticed that would also work. It makes the algorithm easier to understand when you exercise it by hand:

public static boolean isUniqueChars(String str) {
        if (str.length() > 26) { // Only 26 characters
            return false;
        }
        int checker = 0;
        for (int i = 0; i < str.length(); i++) {
            int val = str.charAt(i) - 'a';
            int newValue = Math.pow(2, val) // int newValue = 1 << val
            if ((checker & newValue) > 0) return false;
            checker += newValue // checker |= newValue
        }
        return true;

When we get the value of `val` (0-25), we could either shift 1 to the right by the value of `val`, or we could use the power of 2s.

Also, for as long as the `((checker & newValue) > 0)` is false, the new checker value is generated when we sum up the old checker value and the `newValue`.

- Waseef Akhtar December 20, 2017 | 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