Solutions for Cracking the Coding Interview

Content

1. Array and String

2. Linked Lists

3. Stack and Queues

4. Tree and Graphs

5. Bit Manipulation

6. Brain Teasers

7. Object Oriented Design

8. Recursion

9. Sorting and Search

10. Mathematical

11. System Design and Memory Limits

12. Testing

13. C++

14. Java

15. Databases

16. Low Level

17. Networking

18. Threads and Locks

19. Moderate

20. Hard



Chapter1

Array and Strings

1.1 Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?

Solution: For simplicity, assume char set if ASCII (if not, we need to increase the storage size. The rest logic would be the same). NOTE: This is a great thing to point out to your interviewer!

public static boolean isUniqueChars(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;
	}
	return true;
}
time: O(n)
space: O(n)

We can reduce the space usage a little bit by using a bit vector. We will assume, in the below code, that the string is only lower case ‘a’ through ‘z’. This will allow us to use just a single int

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;
}
Alternatively, we could do the following:
1. Check every char of the string with every other char of the string for duplicate occurences. This will take O(n^2) time and no space;
2. If we are allowed to destroy the input string, we could sort the string in O(n log n) time and then linearly check the string for neighboring characters that are identical. Careful, though - many sorting algorithms take up extra space.


1.2 Write code to reverse a C-Style String. (C-Style means that “abcd” is represented as five characters, including the null character.)

Solution: This is a classic interview question. The only “gotcha” is to try to do it in place, and to be careful for the null character.

void reverse(char *str) {
	char * end = str;
	char tmp;
	if (str) {
		while (*end) {
			++end;
		}
		--end;
		while (str < end) {
			tmp = *str;
			*str++ = *end;
			*end-- = tmp;
		}
	}
}


1.3 Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the array is not.

FOLLOW UP: Write the test cases for this method.

Solution: First, ask yourself, what does the interviewer mean by an additional buffer? Can we use an additional array of constant size?

Algorithm - No (large) Additional Memory:

  1. For each character, check if it is a duplicate of already found characters.
  2. Skip duplicate characters and update the non duplicate characters.

Time complexity is O(n^2).

public static void removeDuplicates(char[] str) {
	if (str == null) return;
	int len = str.length;
	
	int tail = 1;
	
	for (int i = 1; i < len; ++i) {
		int j;
		for (j = 0; j < tail; ++j) {
			if (str[i] == str[j]) break;
		}
		if(j == tail) {
			str[tail] = str[i];
			++tail;
		}
	}
	str[tail] = 0;
}

Test case:
1. String does not contain any duplicates, e.g.: abcd
2. String contains all duplicates, e.g.: aaaa
3. Null string
4. String with all continuous duplicates, e.g.:aaabbb
5. String with non-contiguous duplicates, e.g.: abababa

Algorithm - With additional memory of constant size

public static void removeDuplicatesEff(char[] str) {
	if (str == null) return;
	int len = str.length;
	if (len < 2) return;
	boolean[] hit = new boolean[256];
	for (int i = 0; i < 256; ++i) {
		hit[i] = false;
	}
	hit[str[0]] = true;
	int tail = 1;
	for (int i = 1; i < len; ++i) {
		if (!hit[str[i]]) {
			str[tail] = str[i];
			++tail;
			hit[str[i]] = true;
		}
	}
	str[tail] = 0;
}
Test cases:
1. String does not contain any duplicates, e.g. :abcd
2. String contains all duplicates, e.g. : aaaa
3. Null string
4. Empty string
5. String with all continuous duplicates, e.g. : aaabbb
6. String with non-contiguous duplicates, e.g. : abababa


1.4 Write

Back to Content



Chapter2

Linked Lists

Back to Content



Chapter3

Stack and Queues

Back to Content



Chapter4

Tree and Graphs

Back to Content



Chapter5

Bit Manipulation

Back to Content



Chapter6

Brain Teasers

Back to Content



Chapter7

Object Oriented Design

Back to Content



Chapter8

Recursion

Back to Content



Chapter9

Sorting and Searching

Back to Content



Chapter10

Mathematical

Back to Content



Chapter11

System Design and Memory Limits

Back to Content



Chapter12

Testing

Back to Content



Chapter13

C++

Back to Content



Chapter14

Java

Back to Content



Chapter 15 Databases

Tree and Graphs

Back to Content



Chapter16

Low Level

Back to Content



Chapter17

Networking

Back to Content



Chapter18

Threads and Locks

Back to Content



Chapter19

Moderate

Back to Content



Chapter20

Hard

Back to Content







public class Teisei {
    public static void main(String args[]){
        System.out.println("Hello world! This is Teisei's blog");
    }
}
Tree and Graphs

Back to Content

By the way, if you found a typo, please fork and edit this post. Thank you so much! This post is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

If you like this post or if you use one of the Open Source projects I maintain, say hello by email. There is also my Amazon Wish List. Thank you ♥

Comments

Fork me on GitHub