ctci/01. Arrays and strings
anebz 6f1518cc33 hackerrank weighted uniform string 2019-12-10 17:22:44 +01:00
..
hackerrank hackerrank weighted uniform string 2019-12-10 17:22:44 +01:00
1.1. is_unique.cpp folder renaming 3 2019-04-10 12:40:32 +02:00
1.1. is_unique.md folder renaming 3 2019-04-10 12:40:32 +02:00
1.1. is_unique.py folder renaming 3 2019-04-10 12:40:32 +02:00
1.2. check_permutation.md folder renaming 3 2019-04-10 12:40:32 +02:00
1.2. check_permutation.py folder renaming 3 2019-04-10 12:40:32 +02:00
1.3. replace_char.java folder renaming 3 2019-04-10 12:40:32 +02:00
1.3. replace_char.md folder renaming 3 2019-04-10 12:40:32 +02:00
1.3. replace_char.py folder renaming 3 2019-04-10 12:40:32 +02:00
1.4. palindrome_permutation.cpp folder renaming 3 2019-04-10 12:40:32 +02:00
1.4. palindrome_permutation.java folder renaming 3 2019-04-10 12:40:32 +02:00
1.4. palindrome_permutation.md folder renaming 3 2019-04-10 12:40:32 +02:00
1.4. palindrome_permutation.py folder renaming 3 2019-04-10 12:40:32 +02:00
1.5. levenshtein.java folder renaming 3 2019-04-10 12:40:32 +02:00
1.5. levenshtein.md folder renaming 3 2019-04-10 12:40:32 +02:00
1.5. levenshtein.py folder renaming 3 2019-04-10 12:40:32 +02:00
1.6. string_compression.java folder renaming 3 2019-04-10 12:40:32 +02:00
1.6. string_compression.md folder renaming 3 2019-04-10 12:40:32 +02:00
1.6. string_compression.py folder renaming 3 2019-04-10 12:40:32 +02:00
1.7. matrix_rotation.java folder renaming 3 2019-04-10 12:40:32 +02:00
1.7. matrix_rotation.md folder renaming 3 2019-04-10 12:40:32 +02:00
1.7. matrix_rotation.py folder renaming 3 2019-04-10 12:40:32 +02:00
1.8. zero_matrix.java folder renaming 3 2019-04-10 12:40:32 +02:00
1.8. zero_matrix.md folder renaming 3 2019-04-10 12:40:32 +02:00
1.9. string_rotation.java folder renaming 3 2019-04-10 12:40:32 +02:00
1.9. string_rotation.md folder renaming 3 2019-04-10 12:40:32 +02:00
1.9. string_rotation.py folder renaming 3 2019-04-10 12:40:32 +02:00
README.md Chapter 2 readme 2019-07-06 12:46:37 +02:00

README.md

Chapter 1: Arrays and strings

1.1. Hash tables

data structure that maps keys to values for highly efficient lookup. In this example we use an array of linked lists and a hash code function. To insert a key and value:

  1. Compute the key's hash code, which will usually be an int or long. Two different keys can have the same hash code, because there can be infinite keys but there is only a finite number of integers.
  2. Map the hash code to an index in the array, for example with hash(key) % array_length. Two different hash codes can have the same index.
  3. At this index there's a liked list of keys and values, and we store the key value pair in this index. A linked list avoids collissions, you can have two different keys with the same hash code and two different hash codes mapping to the same index.

To retreat the value from a key, repeat the process. Calculate hash code, compute the index, search in the linked list.

If the number of collissions is very high, the worst case runtime is O(N), where N = # keys. But if we implement a good system keeping collissions to a minimum, lookup is O(1).

Alternatively we can implement a lookup system with a balanced binary search tree, giving us O(logN) lookup time. It uses less space because we don't have to allocate a large array, and we can iterate through the keys in order.

1.2. ArrayList and resizable arrays

In some languages arrays/lists are automatically resizable, but it isn't in others such as Java. An ArrayList is an array that resizes itself as needed while still providing O(1) access. A typical implementation is doubling the array size when it gets full, but others increase 50% or some other number. Each resizing takes O(n) time, but happens so rarely that its amortized insertion time is still O(1).

Why is the amortized insertion runtime O(1)

Because inserting N elements, we insert 1 + 2 + N/8 + N/4 + N/2 = N (roughly). Insertin N elements takes O(N), inserting one takes O(1), even though in the worst case it'll be O(N)

1.3. StringBuilder

We want to concatenate a list of strings, there are n strings each with length x.

String joinWords(String[] words) {
    String sentence = "";
    for (String w : words) {
        sentence = sentence + w;
    }
    return sentence;
}

On each concatenation it creates a new copy of the string. It copies x characters, then 2x, until nx. The total time is O(1x+ 2x + ... + nx) = O(xn(n+1)/2) = O(xn2).

StringBuilder creates a resizable array of all the strings, copying them back to a string only when necessary.

String joinWords(String[] words) {
    StringBuilder sentence = new StringBuilder();
    for (String w : words) {
        sentence.append(w);
    }
    return sentence.toString();
}

Resources