diff --git a/README b/README index 2558bc4..b4d27d8 100644 --- a/README +++ b/README @@ -10,16 +10,16 @@ C1: General 1) Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7. 2) Write a regular expression which matches a email address. (Ramp up Regular Expression) 3) Write a function f(a, b) which takes two character string arguments and returns a string containing only the characters found in both strings in the order of a. Write a version which is order N-squared and one which is order N. - 4) Write a function (with helper functions if needed) called to Excel that takes an excel column value (A,B,C,D…AA,AB,AC,… AAA..) and returns a corresponding integer value (A=1,B=2,… AA=26..). + 4) Write a function (with helper functions if needed) called to Excel that takes an excel column value (A,B,C,D��A,AB,AC,��AAA..) and returns a corresponding integer value (A=1,B=2,��AA=26..). 5) You have a stream of infinite queries (ie: real time Google search queries that people are entering). Describe how you would go about finding a good estimate of 1000 samples from this never ending set of data and then write code for it. 6) Write some code to reverse a string. 7) Write some code to find all permutations of the letters in a particular string. 8) What method would you use to look up a word in a dictionary? 9) You have eight balls all of the same size. 7 of them weigh the same, and one of them weighs slightly more. How can you fine the ball that is heavier by using a balance and only two weighings? - 10) There is a linked list of numbers of length N. N is very large and you don’t know N. You have to write a function that will return k random numbers from the list. Numbers should be completely random. Hint: 1. Use random function rand() (returns a number between 0 and 1) and irand() (return either 0 or 1) 2. It should be done in O(n). + 10) There is a linked list of numbers of length N. N is very large and you don't know N. You have to write a function that will return k random numbers from the list. Numbers should be completely random. Hint: 1. Use random function rand() (returns a number between 0 and 1) and irand() (return either 0 or 1) 2. It should be done in O(n). 11) There is an array A[N] of N numbers. You have to compose an array Output[N] such that Output[i] will be equal to multiplication of all the elements of A[N] except A[i]. For example Output[0] will be multiplication of A[1] to A[N-1] and Output[1] will be multiplication of A[0] and from A[2] to A[N-1]. Solve it without division operator and in O(n). 12) Find or determine non existence of a number in a sorted list of N numbers where the numbers range over M, M>> N and N large enough to span multiple disks. Algorithm to beat O(log n) bonus points for constant time algorithm. - 13) You are given an array [a1 To an] and we have to construct another array [b1 To bn] where bi = a1*a2*…*an/ai. you are allowed to use only constant space and the time complexity is O(n). No divisions are allowed. + 13) You are given an array [a1 To an] and we have to construct another array [b1 To bn] where bi = a1*a2*��an/ai. you are allowed to use only constant space and the time complexity is O(n). No divisions are allowed. 14) Given two sequences of items, find the items whose absolute number increases or decreases the most when comparing one sequence with the other by reading the sequence only once. 15) Given that you have one string of length N and M small strings of length L. How do you efficiently find the occurrence of each small string in the larger one? 16) Given a file of 4 billion 32-bit integers, how to find one that appears at least twice? @@ -46,17 +46,17 @@ C2: CS Theory 5) Implement division (without using the divide operator, obviously). 6) Suppose you have given N companies, and we want to eventually merge them into one big company. How many ways are theres to merge? 7) How long it would take to sort 1 trillion numbers? Come up with a good estimate. - 8) What’s the difference between a hashtable and a hashmap? + 8) What's the difference between a hashtable and a hashmap? C3: List - 1) You are given a list of numbers, and the list is circular list. Write the most efficient algorithm to find the minimum # in this list. Find any given # in the list. The numbers in the list are always increasing but you don’t know where the circular list begins, ie: 38, 40, 55, 89, 6, 13, 20, 23, 36. + 1) You are given a list of numbers, and the list is circular list. Write the most efficient algorithm to find the minimum # in this list. Find any given # in the list. The numbers in the list are always increasing but you don�� know where the circular list begins, ie: 38, 40, 55, 89, 6, 13, 20, 23, 36. 2) Write a function to find the middle node of a single link list. 3) Given two linked lists, return the intersection of the two lists: i.e. return a list containing only the elements that occur in both of the input lists. C4: Array 1) Suppose you have an NxN matrix of positive and negative integers. Write some code that finds the sub-matrix with the maximum sum of its elements. 2) Given an array, find the longest continuous increasing subsequence. - 3) You are given with three sorted arrays ( in ascending order), you are required to find a triplet ( one element from each array) such that distance is minimum. Distance is defined like this : If a[i], b[j] and c[k] are three elements then distance=max(abs(a[i]-b[j]),abs(a[i]-c[k]),abs(b[j]-c[k]))” Please give a solution in O(n) time complexity + 3) You are given with three sorted arrays ( in ascending order), you are required to find a triplet ( one element from each array) such that distance is minimum. Distance is defined like this : If a[i], b[j] and c[k] are three elements then distance=max(abs(a[i]-b[j]),abs(a[i]-c[k]),abs(b[j]-c[k]))��Please give a solution in O(n) time complexity 4) Given an array whose elements are sorted, return the index of a the first occurrence of a specific integer. Do this in sub-linear time. I.e. do not just go through each element searching for that element. 5) Given an array, print the array elements cyclicly from outside to center. 6) Given an array, use Binary Search to find a given element @@ -69,7 +69,7 @@ C4: Array 13) Given an array containing both positive and negative numbers, find the sub array whose elements' sum is maximum. 14) Given a set S, find all the maximal subsets whose sum <= k. 15) Find the uniq amount of absolute values in a given sorted array - + C5: Tree 1) Describe the algorithm for a depth-first graph traversal. 2) Tree search algorithms. Write BFS and DFS code, explain run time and space requirements. Modify the code to handle trees with weighted edges and loops with BFS and DFS, make the code print out path to goal state. @@ -87,7 +87,7 @@ C7: Stack C8: Sorting 1) If you have 1 million integers, how would you sort them efficiently? - 2) You are given a small sorted list of numbers, and a very very long sorted list of numbers – so long that it had to be put on a disk in different blocks. How would you find those short list numbers in the bigger one? + 2) You are given a small sorted list of numbers, and a very very long sorted list of numbers ��so long that it had to be put on a disk in different blocks. How would you find those short list numbers in the bigger one? 3) What sort would you use if you had a large data set on disk and a small amount of ram to work with? 4) What sort would you use if you required tight max time bounds and wanted highly regular performance. @@ -95,7 +95,12 @@ C9: Design Patterns and OOP 1) Design a class library for writing card games. C10: System Design - 1) Design and describe a system/application that will most efficiently produce a report of the top 1 million Google search requests. These are the particulars: a) You are given 12 servers to work with. They are all dual-processor machines with 4Gb of RAM, 4x400GB hard drives and networked together.(Basically, nothing more than high-end PC’s); b) The log data has already been cleaned for you. It consists of 100 Billion log lines, broken down into 12 320 GB files of 40-byte search terms per line. c) You can use only custom written applications or available free open-source software. + 1) Design and describe a system/application that will most efficiently produce a report of the top 1 million Google search requests. These are the particulars: a) You are given 12 servers to work with. They are all dual-processor machines with 4Gb of RAM, 4x400GB hard drives and networked together.(Basically, nothing more than high-end PC��); b) The log data has already been cleaned for you. It consists of 100 Billion log lines, broken down into 12 320 GB files of 40-byte search terms per line. c) You can use only custom written applications or available free open-source software. 2) Write a program for displaying the ten most frequent words in a file such that your program should be efficient in all complexity measures. 3) Create a fast cached storage mechanism that, given a limitation on the amount of cache memory, will ensure that only the least recently used items are discarded when the cache memory is reached when inserting a new item. It supports 2 functions: String get(T t) and void put(String k, T t). - 4) Remove duplicated lines in a very large block of text. \ No newline at end of file + 4) Remove duplicated lines in a very large block of text. + +C11: String + 1) Implement an algorithm to determine if a string contains all 26 characters. What if you can not use additional data structures? + 2) Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? + \ No newline at end of file diff --git a/src/com/interview/datastructures/string/C11_1_All26CharacterChecker.java b/src/com/interview/datastructures/string/C11_1_All26CharacterChecker.java new file mode 100644 index 0000000..630a8a3 --- /dev/null +++ b/src/com/interview/datastructures/string/C11_1_All26CharacterChecker.java @@ -0,0 +1,63 @@ +package com.interview.datastructures.string; + +/** + * Problems: + * Implement an algorithm to determine if a string has all 26 characters. + * What if you can not use additional data structures? + * + * Solutions: + * 1.Define a byte array of 26 characters, and scan the string and mark the flag to 1, + * check if all the byte is marked to 1. + * time: O(N), space: O(26) + * + * + * @author stefanie + * + */ +public class C11_1_All26CharacterChecker { + public boolean check_solution1(String str){ + int base = (int) 'A'; + boolean[] flag = new boolean[26]; + + for(char character : str.toCharArray()){ + int index = -1; + if(character >= 'A' && character <= 'Z'){ //if capital + index = (int) character - base; + } else if(character >= 'a' && character <= 'z') { + index = (int) character - base - 32; + } else { //if not character omit it + continue; + } + flag[index] = true; + } + + for(boolean each : flag){ + if(each == false){ + return false; + } + } + return true; + } + + public static void main(String[] args){ + C11_1_All26CharacterChecker checker = new C11_1_All26CharacterChecker(); + String str1 = "abcdefghijklmnopqrstuvwxyz"; + System.out.println(str1); + System.out.println(checker.check_solution1(str1)); + String str2 = "AbcdefghijkLmnoPqrstuVwxyz"; + System.out.println(str2); + System.out.println(checker.check_solution1(str2)); + String str3 = "QWERTYUIOPASDFGHJKLZXCVBNM"; + System.out.println(str3); + System.out.println(checker.check_solution1(str3)); + String str4 = "sujpoubjwy72bbsj"; + System.out.println(str4); + System.out.println(checker.check_solution1(str4)); + String str5 = "AbcdefghijkLmnoPqstuVwxyz"; + System.out.println(str5); + System.out.println(checker.check_solution1(str5)); + String str6 = "Abcdefghijk67892LmnoPqrstuVwxyz12345"; + System.out.println(str6); + System.out.println(checker.check_solution1(str6)); + } +} diff --git a/src/com/interview/datastructures/string/C11_2_UniqueCharacterChecker.java b/src/com/interview/datastructures/string/C11_2_UniqueCharacterChecker.java new file mode 100644 index 0000000..6420ab1 --- /dev/null +++ b/src/com/interview/datastructures/string/C11_2_UniqueCharacterChecker.java @@ -0,0 +1,44 @@ +package com.interview.datastructures.string; + +/** + * Problem: + * Implement an algorithm to determine if a string has all unique characters. + * What if you can not use additional data structures? + * + * Solutions: + * 1. assume char set is ASCII, use a 256 char array as a flag to check if there is duplicate characters + * time: O(N), space: O(256) + * 2. if not allow to use extra space, could check every character in the string if there is duplicate char. + * time: O(N^2), space: 1 + * if could destroy the string, could sort then check if have duplicate char + * time: O(NlogN), space: 1 + * @author stefanie + * + */ +public class C11_2_UniqueCharacterChecker { + + public boolean check_solution1(String str){ + boolean[] flag = new boolean[256]; + for(int i = 0; i < str.length(); i++){ + int ch = str.charAt(i); + if(flag[ch]) + return false; + else + flag[ch] = true; + } + return true; + } + + public static void main(String[] args){ + C11_2_UniqueCharacterChecker checker = new C11_2_UniqueCharacterChecker(); + String str1 = "stefaniezhao"; + System.out.println(str1); + System.out.println(checker.check_solution1(str1)); + String str2 = "STEFANIezhao"; + System.out.println(str2); + System.out.println(checker.check_solution1(str2)); + String str3 = "1234567890STEFANIezhao"; + System.out.println(str3); + System.out.println(checker.check_solution1(str3)); + } +}