27.10.2014 Views

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Solutions</strong> to Chapter 13 | C++<br />

13.2 Compare <strong>and</strong> contrast a hash table vs. an STL map. How is a hash table implemented?<br />

If <strong>the</strong> number of inputs is small, what data structure options can be used instead of<br />

a hash table?<br />

SOLUTION<br />

pg 76<br />

Compare <strong>and</strong> contrast Hash Table vs. STL map<br />

In a hash table, a value is stored by applying hash function on a key. Thus, values are not<br />

stored in a hash table in sorted order. Additionally, since hash tables use <strong>the</strong> key to find <strong>the</strong><br />

index that will store <strong>the</strong> value, an insert/lookup can be done in amortised O(1) time (assuming<br />

only a few collisions in <strong>the</strong> hashtable). One must also h<strong>and</strong>le potential collisions in a<br />

hashtable.<br />

In an STL map, insertion of key/value pair is in sorted order of key. It uses a tree to store<br />

values, which is why an O(log N) insert/lookup is required. There is also no need to h<strong>and</strong>le<br />

collisions. An STL map works well for things like:<br />

»»<br />

find min element<br />

»»<br />

find max element<br />

»»<br />

print elements in sorted order<br />

»»<br />

find <strong>the</strong> exact element or, if <strong>the</strong> element is not found, find <strong>the</strong> next smallest number<br />

How is a hash table implemented?<br />

1. A good hash function is required (e.g.: operation % prime number) to ensure that <strong>the</strong><br />

hash values are uniformly distributed.<br />

2. A collision resolving method is also needed: chaining (good for dense table entries),<br />

probing (good for sparse table entries), etc.<br />

3. Implement methods to dynamically increase or decrease <strong>the</strong> hash table size on a given<br />

criterion. For example, when <strong>the</strong> [number of elements] by [table size] ratio is greater<br />

than <strong>the</strong> fixed threshold, increase <strong>the</strong> hash table size by creating a new hash table <strong>and</strong><br />

transfer <strong>the</strong> entries from <strong>the</strong> old table to <strong>the</strong> new table by computing <strong>the</strong> index using<br />

new hash function.<br />

What can be used instead of a hash table, if <strong>the</strong> number of inputs is small?<br />

You can use an STL map. Although this takes O(log n) time, since <strong>the</strong> number of inputs is<br />

small, this time is negligible.<br />

CareerCup.com<br />

2 1 6

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!