Google Interview Question for Software Engineer / Developers


Country: United States




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

How about bit-wise XOR all chars in a word? Very easy implementation but few flows:
-Even number of repeated letters will cancel itself though. "aka" will collide "k"
-As ascii values of letters are always between same interval mapping image of the hash will not use [0,255] range homogeneously.

char hash(string word){
	char xorsum=0
	for (int i=0;i<word;i++){
	xorsum^=word[i];
	}
return xorsum;
}

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

how can I delete this question?

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

Is there an edit button on the question? There should be here:

"adam2008 on February 11, 2013 in United States Edit | Report Duplicate | Flag "

- Gayle L McDowell February 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

why u require this at all...

coz whole and sole of hash function is to map to some integer value and that is nothing but some location of array.... so that it'll give you nearly constant time in searching, deleting and updating operations....these are dictionary operations....

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

Here is my idea.

First, I think we can use Huffman Encode to give each letter the specific representing bits. I think the avg bits needed is about 3. (because there are 26 letters, so you can use 5 bits to represent a letter in general). Suppose we need 4 bits to represent a letter after encoding. Then if we use 4 Bytes to represent each word. The max length with this method can hold the word which has no more 8 letters. I think that's a threshold, because the words longer than 8 letters are much less. So if the word is longer than 8 letters, we can map it with the first 8 letters(of course, need collision resolved but the length of collision nodes won't be very large).
The problem is the space needed is still large.
Waiting others good ideas.

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

Hash function for a string:

In Python:

def do_hash( my_str ):
	# we want it to fit into 1 byte... (it would be better to use the first prime number that is less than 256)
	return (hash(my_str, 256 ))
def hash( my_str, hash_size ):
      hash_val = 0
      alphabet_size = 26

      for curr_char in my_str:
		if curr_char >= 'A' and curr_char <= 'Z':
			char_val = ord(curr_char) - ord('A'):
	        elif curr_char >= 'a' and curr_char <= 'z':
			char_val = ord(curr_char) - ord('a')
		else:
			raise Exception("Not a string...")

                hash_val += hash_val*26 + char_val

	return hash_val % hash_size

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

The magic 33 method, but why you want to hash it to byte? How about 32-bit?

long HashWord (char* str) {
long hash = 0;
while (*str)
  hash = hash << 5 + hash + *str++;
return hash;
}

- zippon.wu March 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just use Java HashCode algorithm for string.

- Riyad Parvez July 26, 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