You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

Suppose you have n versions [1, 2, …, n] and you want to find out the first bad one, which causes all the following ones to be bad.

You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

Solution:

This is solved using binary search since we can see the list of versions as an array of 0’s and 1’s in sorted order having zeroes as good versions and ones as bad versions, so our task is to find the first occurrence of 1, so how can we find the first occurrence of a number on a sorted array?… Binary search!!

So we basically…

1.- Create to pointers, one pointing to the beginning of the list (start = 0) and another one to the end of the list (end = n)

1.- Check if the middle element (middle = start + end / 2) is a correct version

2.- If it is a correct version we now know we don’t need the second half of the list so we go left (end = middle-1), otherwise we go right (start = middle+1)

3.- Repeat steps 1 and 2 until there is no more elements to check (while start <= end)

def firstBadVersion(self, n): start = 1 end = n middle = 0 while(start <= end): middle = (start + end) / 2 if isBadVersion(middle): end = middle - 1 else: start = middle + 1 if isBadVersion(middle): return middle else: return middle + 1

Runtime complexity: O(log n) since we cut the array in half at every step, so for an array of size 10 we just need log(10) = 3 operations to find the first bad version

]]>

Given n nodes, how many binary trees can be formed?

example.

for n = 3 there are 5 different binary trees that can be formed:

1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3

Solution:

If we have n nodes 1, 2, 3, …, n we know that in some point every node is gonna be the root, and we also know that every node has two sub-trees, the left sub-tree and the right sub-tree, the key here is RECURSION, we can split this problem into different sub problems, let’s see it with an example:

for n = 1 we have ONE possible tree.

for n = 2 we have TWO possible trees:

1 1 \ / 2 2

for n = 3 we have the following options:

A) zero nodes at the left sub-tree and two nodes at the right sub-tree

B) one node at left and one node at right

C) zero nodes at the right sub-tree and two nodes at the left sub-tree

root root root / \ / \ / \ numTrees(0) numTrees(2) numTrees(1) numTrees(1) numTrees(2) numTrees(0) 1 * 2 + 1 * 1 + 2 * 1 = 5 different trees

So it’s clear we are going to solve this problem recursively:

def numTrees(self, n): if n <= 1: return 1 res = 0 for i in range(n): res += self.numTrees(i) * self.numTrees(n - i - 1) return res

]]>

Given two strings A and B, find the minimum number of operations needed to transform A into B, there are only three valid operations:

-Replace a character

-Delete a character

-Insert a character

Examples:

A = “hello”

B = “hola”

result = 3, the operations were:

1.- replace ‘e’ with ‘o’ -> “hollo”

2.- replace ‘l’ with ‘a’ -> “holao”

3.- remove ‘o’ -> “hola”

Solution:

So for this problem we have two strings A and B, let’s work with the following example:

A = "abc" i B = "cba" j

Remember that we want to transform A into B, any operation has to be done over A.

We are gonna use indexes i and j to loop through the strings and compare them character by character so that at every step we have the following options:

First index i and index j are both at position 0, so we have ‘a’ and ‘c’, since they are different we have three options here:

1.- remove ‘a’ (i + 1, j) -> “bc”

2.- insert ‘c’ into A (i, j + 1) -> “cabc”

3.- replace ‘a’ with ‘c’ (i + 1, j + 1) -> “cbc”

Why don’t we just try them all?

int minDistance(String word1, String word2) { int dp[][] = new int[word1.length() + 1][word2.length() + 1]; for(int i = 0; i <= word1.length(); ++i) for(int j = 0; j <= word2.length(); ++j) dp[i][j] = -1; return getDistance(word1, word2, word1.length(), word2.length(), dp); } int getDistance(String word1, String word2, int i, int j, int [][]dp) { if(i == 0) return j; if(j == 0) return i; if(dp[i][j] >= 0) return dp[i][j]; if(word1.charAt(i - 1) == word2.charAt(j - 1)) return dp[i][j] = getDistance(word1, word2, i - 1, j - 1, dp); else return dp[i][j] = 1 + Math.min(getDistance(word1, word2, i - 1, j - 1, dp), Math.min(getDistance(word1, word2, i - 1, j, dp), getDistance(word1, word2, i, j - 1, dp))); }

The dp table is very important in this problem, it is used to avoid a sub task to be computed more than once, otherwise our program would run very very slow.

]]>

You have an array A of size n – 1 containing numbers from 1 to n so there is one missing number, find it!

First Approach using extra memory:

We could create a boolean array B of size n + 1 and set to true the index of every number found in A, at the end our boolean array will have only one element in false, that’s the missing number.

int findMissingNumber(int A[], int n) { boolean B[] = new boolean[n + 1]; for(int i = 0; i < A.length; ++i) B[A[i]] = true; int missing = 0; for(int i = 1; i < B.length; ++i) if(!B[i]) missing = i; return missing; }

Second Approach without extra memory:

Another approach that avoids the use of extra memory is to sum up all the elements in the array and subtract that value from the total sum which can be obtained with the following formula (n * (n + 1)) / 2

int findMissingNumber(int A[], int n) { int total = (n * (n + 1)) / 2; int sum = 0; for(int i = 0; i < A.length; ++i) sum += A[i]; return total - sum; }

This solution only uses constant space but the problem is that if the numbers are too large we could overflow the variable sum since an integer has a max value.

Third approach, XOR:

This approach is based on the XOR operation, remember:

A B | A XOR B

0 0 | 0

0 1 | 1

1 0 | 1

1 1 | 0

so:

A ^ 0 = A

A ^ A = 0

A ^ B = C

C ^ A = B

The key is to realize that if we XOR the same number twice we are gonna get 0 (equal numbers are removed), but if we XOR a number once then this number is kept, so we can XOR all numbers from 1 to n and then xor all numbers in our array, at the end we are gonna have the missing number since all duplicated numbers were removed, here the code:

int findMissingNumber(int A[], int n) { int xor = 0; for(int i = 1; i <= n; ++i) xor ^= i; for(int i = 0; i < A.length; ++i) xor ^= A[i]; return xor; }

This solution doesn’t have the problem of integer overflow like the previous one and needs only linear time and constant space

]]>

Given a non negative number n, generate all possible combinations of strings that can be formed by n-pairs of well-formed parentheses, for example:

for n = 2

a solution is: “(())”, “()()”

and for n = 3

a solution set is: “((()))”, “(()())”, “(())()”, “()(())”, “()()()”

Solution:

We could use a simple Depth First Search (DFS) approach to recursively build the set of solutions, see the solution code below and then read the explanation:

void generateParentheses(int n) { dfs("", n, n); } void dfs(String s, int left, int right) // build the set of solutions using DFS approach { if(left == 0 && right == 0) // BASE CASE: there is no more parentheses to add? we have a solution! System.out.println(s); if(left > 0) // while we have left parentheses to add, just add them dfs(s + "(", left - 1, right); // we call our function recursively with a left parentheses added if(right > left) // We are gonna add a right parentheses if we have more right parentheses than left ones dfs(s + ")", left, right - 1); }

Let’s work on the simplest example to explain how it works:

for n = 2 we know that we have 2 left parentheses and 2 right parentheses so for each recursive call we are going to take 2 decisions:

1.- Can we put a left parentheses?

2.- Can we put a right parentheses?

How do we know if we are able to put a left or right parentheses at each recursive call?

We can put a left parentheses as long as we have at least one left parentheses available but for the right ones we can add them only when we have more right parentheses available because we know that we have already added the corresponding left parentheses before adding the right one.

Do you have any question/comment/suggestion? Please leave your comment below.

]]>

Given an undirected graph write a function that returns a CLONE of that graph, example of an undirected graph:

1 ———- 2

| /

| /

3

Each node has a label and a list of neighbors (represented as an ArrayList).

**Solution code**

map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>(); //This map is gonna be used to check if a node has been already created, //we use the original node as a key and the clone node as the value. UndirectedGraphNode clone(UndirectedGraphNode node) { if(map.containsKey(node)) //if the current node has been already cloned then just return the cloned node return map.get(node); UndirectedGraphNode newNode = new UndirectedGraphNode(node.label); // create the clone node map.put(node, newNode); // put the clone into the map ArrayList<UndirectedGraphNode> nei = new ArrayList<UndirectedGraphNode>(); //create a new arraylist of neighbors for the new node // loop through the neighbors of the original node and call our function recursively to build the new list of neighbors for(UndirectedGraphNode n : node.neighbors) nei.add(clone(n)); newNode.neighbors = nei; return newNode; }

**Explanation**:

It is basically a DFS traversal (http://en.wikipedia.org/wiki/Depth-first_search), the clone function is called for every node and it returns the new cloned node with it’s corresponding list of neighbors, the key part here is that we need to keep track of those nodes that we have already cloned to avoid infinite loops, this is done with the HasMap.

]]>

Given a String and a dictionary of words, write a program that returns true if the given string can be formed by concatenating one or more of the words in the dictionary, eg.

Given the string S = “algorithmstuff”

and the dictionary dic = {“algorithm”, “stuff”}

Our function must return true since S can be formed by concatenating “algorithm” + “stuff”

but given S = “algorithmstuffisgood”

and dic = {“algorithms”, “good”, “stuff”} must return false since there’s no way to build S from the strings in the dictionary.

Solution

This is a very good problem for a programming interview since it has one “lazy solution” which is very inefficient but it can be converted to a very fast algorithm with just a simple modification, so you could think about the most obvious solution even if it’s slow and once you got a working solution you can think about how to improve it.

This problem is clearly a good candidate for a recursive solution, for each recursive call the idea is to start checking if the first character is in our dictionary, if so, we call the function recursively but now with string S starting with the next character, otherwise we check if the first two characters form a valid word and so on, for instance:

S = “hithere”

dic = { “there”, “hi”}

We start with the string “hithere” at index 0, so we check if the substring “h” is a valid word, since it’s not we continue now with the next character which is “i”, now we check if the substring “hi” is a valid word in the dictionary, it is so we know that “hi” can be formed by concatenating one or more words of the dictionary so we now call the function recursively but starting with the next index which is 2, so now we are gonna have the same function with the substring “there”, we are gonna stop when we reach the end of the string S.

This is basically a DFS algorithm (http://en.wikipedia.org/wiki/Depth-first_search) , here the code:

public boolean stringBreak(String s, Set<String> dict) { return dfs(s, 0, dict); } boolean dfs(String s, int i, Set<String> dict) { if(dict.contains(s.substring(i))) return true; for(int j = i; j < s.length(); ++j) if(dict.contains(s.substring(i, j + 1))) if(dfs(s, j + 1, dict, set)) return true; return false; }

The solution above works! but it’s too slow since there are many overlapping calls (it does the same thing many times), let me show you with an example:

let’s say we have the string S = “helloworld”

and dict = {“he”, “e”, “h”}

Our recursive function is gonna build a tree like this:

helloworld / \ h-elloworld he-lloworld | e-lloworld

As you can see the substring “lloworld” is gonna be computed two times and the result is gonna be the same, the problem here is that we could end up with too many overlapping calls which would be a waste of time. So, how can we avoid overlapping calls?

The solution is to have a record of which substrings we have already computed so that when our function is called again for the same substring we already know the answer, this technique is called MEMOIZATION (http://en.wikipedia.org/wiki/Memoization), Here is the code that shows this optimization.

public boolean stringBreak(String s, Set<String> dict) { HashSet<Integer> memo = new HashSet<Integer>(); // We just need to keep track of those indexes that we have already computed and the result is false return dfs(s, 0, dict, memo); } boolean dfs(String s, int i, Set<String> dict, HashSet<Integer> memo) { if(dict.contains(s.substring(i))) return true; if(memo.contains(i)) // if we have already computed the result for this substring we just return the answer return false; //otherwise, compute the answer for this substring for(int j = i; j < s.length(); ++j) if(dict.contains(s.substring(i, j + 1))) if(dfs(s, j + 1, dict, set)) return true; //we just store the results for substrings which result is false memo.add(i); return false; }

]]>

Where each digit corresponds to a set of three or four letters.

Problem Description:

Write a program that, given a phone number, return all possible letter combinations that can be formed. Eg.

Given 23

The following letter combinations can be formed: “AD”, “AE”, “AF”, “BD”, “BE”, “BF”, “CD”, “CE”, “CF”.

Given 234

The following letter combinations can be formed:

“ADG”,”ADH”,”ADI”,”AEG”,”AEH”,”AEI”,”AFG”,

“AFH”,”AFI”,”BDG”,”BDH”,”BDI”,”BEG”,”BEH”,

“BEI”,”BFG”,”BFH”,”BFI”,”CDG”,”CDH”,”CDI”,

“CEG”,”CEH”,”CEI”,”CFG”,”CFH”,”CFI”.

Solution

This problem requires a complete search solution since we need to explore all possible letter combinations, more specifically we are gonna use a well known technique called BACKTRACKING (http://en.wikipedia.org/wiki/Backtracking), it is basically a recursive function that builds a tree “on the fly”, at each recursive call it gets the set of letters that corresponds to the current digit and calls itself recursively for every letter but now with the next digit as parameter.

//Global variables StringBuilder tempSolution; ArrayList<String> result; String letters[]; ArrayList<String> letterCombinations(String digits) { //Our map, we use the digit as an index to get the corresponding set of letters letters = new String[]{"", "", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ"}; result = new ArrayList<String>(); //tempSolution is used to build the solution on the fly tempSolution = new StringBuilder(""); //here we call dfs function starting with digit at position zero backtracking(digits, 0); return result; } void backtracking(String digits, int i) // variable i points the current digit { //We get the set of corresponding letters for the current digit String setOfLetters = letters[digits.charAt(i) - 48]; //Now we loop through the set of letters for(int a = 0; a < setOfLetters.length(); ++a) { //Append each one of the letters to our temporary solution tempSolution.append(setOfLetters.charAt(a)); //have we finished?, if so, add this temporary combination to our resulting ArrayList if(i == digits.length() - 1) result.add(tempSolution.toString()); else //otherwise call this function recursively but now with the next digit as current digit backtracking(digits, i + 1); //Here is where the magic of backtracking happens tempSolution.deleteCharAt(st.length() - 1); } }

It runs in O(3 ^ n), where n is the number of digits of the phone number.

]]>

Today I’m gonna share a solution for a very common problem for programming interviews, how to print a 2D matrix in spiral order.

Problem description:

Given an M x N matrix, print all the elements in spiral order, for instance:

[[1, 2, 3, 4] [2, 4, 9, 5] [9, 8, 7, 6]]

print: 1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 4, 9

This is an easy problem which interviewers use to test how clean you are able to code and how good you are at manipulating indexes, you need to be very careful when you try to come up with a solution for this problem, remember the KISS principle: KEEP IT SIMPLE & SHORT. Talking about programming interviews, usually when your idea requires more than 30 – 40 lines of code it meas that it’s not a good solution.

Solution:

The idea is to have four indexes: top, down, left and right which are gonna be used to point where we are gonna start printing rows and columns, when top > down or left > right then we are done.

void spiralOrder(int[][] matrix) { if(matrix.length == 0) return; // Initialize our four indexes int top = 0; int down = matrix.length - 1; int left = 0; int right = matrix[0].length - 1; while(true) { // Print top row for(int j = left; j <= right; ++j) System.out.print(matrix[top][j] + " "); top++; if(top > down || left > right) break; //Print the rightmost column for(int i = top; i <= down; ++i) System.out.print(matrix[i][right] + " "); right--; if(top > down || left > right) break; //Print the bottom row for(int j = right; j >= left; --j) System.out.print(matrix[down][j] + " "); down--; if(top > down || left > right) break; //Print the leftmost column for(int i = down; i >= top; --i) System.out.print(matrix[i][left] + " "); left++; if(top > down || left > right) break; } }

This solution works for any M x N matrix, time complexity: O(M * N) space complexity: Constant

]]>

Given a string, find the length of the longest substring without repeating characters, for example, s = “abcabc”, the longest substring without repeating characters is “abc” so the length is 3.

First Approach:

Like always, there is a non optimal solution, actually a quadratic solution, just loop the array and for each element loop the array again by keeping track of the elements that we have visited and stop when you find a repeating character, this will give us the longest substring without repeating characters. This solutions works but is too slow, can we do it better? yes.

A better approach:

We just need to look at it a little bit closer to realize that at each time we find a repeating character we don’t need to start again at position i + 1, because we know that all the remaining elements except the element at position i are unique. Let’s clarify this idea with an example:

s = “abcab”

1.- Lets start looping through our array, at first we have “a”, do we have already an “a”?, No. So let’s store “a” and continue with the next element.

2.- Now we have “b”, do we have already a “b” ?, Nope, so we store the “b” and continue.

3.- We get a “c” , we don’t have any “c” already stored, so store it and continue.

4.- Now we get an “a”, do we have already an “a”?, Yes we have. So now we know that so far all the elements are different but the “a” at position 0 and the “a” at position 3, with our first approach we just would start again at position 1, but it’s not necessary because we know that all the remaining elements from index 1 to 3 are different, so we just ignore the “a” at position 0 and continue with the next character. By doing this we are gonna avoid a bunch of operations and now we have a much more efficient algorithm.

Here the working code in Java:

public int lengthOfLongestSubstring(String s) { int max = 0; int count = 0; int a[] = new int[26]; Arrays.fill(a, -1); for(int i = 0; i < s.length(); ++i) { if(a[s.charAt(i) - 'a'] >= 0) { int from = a[s.charAt(i) - 'a']; count = i - a[s.charAt(i) - 'a']; a = new int[26]; Arrays.fill(a, -1); for(int j = from + 1; j <= i; ++j) a[s.charAt(j) - 'a'] = j; } else { count++; a[s.charAt(i) - 'a'] = i; } max = Math.max(max, count); } return max; }

]]>