SlideShare a Scribd company logo
2
Most read
4
Most read
8
Most read
The answer to your interview
question is probably a hash table
intro to hash tables
Amy Hua
Why hash tables?
Great for quickly looking something up. Usually faster than arrays or most anything else.
Big O Performance: O(1) to O(n)
Example applications
For example
• Say you have an array of ~100 items
– To look up item at position 3:
• myarray[3]
• O(1)
• But how do you know that the item you’re
looking for is in position 3?
For example
In a phone book, how do you know Jane Smith is on page 245?
You probably used an index (last name: Smith) to find the page or range of
pages Jane Smith would be located on.
For example
In a phone book, how do you know Jane Smith is on page 245?
LookUpPageByLastName(“Smith”) = page 245
hash function
If you know how to use a phone book,
you know how to use a hash table
Hash table: data structure for quickly looking things up.
Given some key, we can apply a hash function to it to find
the index in the array that we want to access.
hash function: takes a key  returns an index in the array
LookUpPageByLastName(“Smith”) = 245
hash function key index
Example hash function
Given keys
keys: Lizzie McGuire
Fiona Apple
James Bond
Index them by converting their names to integers
Lizzie McGuire  13223
Fiona Apple  12342
James Bond  23523
Divide their numbers by a large enough number (modulo)
Lizzie McGuire  3,223 % 1000 = 3 remainder 0.223
Fiona Apple  4,342 % 1000 = 4 remainder .342
James Bond  9,523 % 1000 = 9 remainder .523
Hash table: stores the key (used to find the index) along with the associated value we’re looking up.
Collisions
Sometimes, more than one name is on a given page: multiple elements can
have the same index. In other words, collisions can occur in our hash table.
How do you handle collisions?
Collisions
Well what do you do when you see multiple names on the same page in a
phone book?
You go through a
linked list
You now know how to use a phone
book.. again
Sources
• http://www.cs.uregina.ca/Links/class-
info/210/Hash/
References
• http://www.youtube.com/watch?v=MfhjkfocRR0&li
st=TLxupPRqcHJiw
• http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/n
otes/07-hashing.pdf
• http://courses.csail.mit.edu/6.006/spring11/rec/rec
05.pdf

More Related Content

PDF
An introduction to MongoDB
PPTX
MongoDB and Indexes - MUG Denver - 20160329
PPTX
Major issues in data mining
PPT
Database testing
PPTX
Basic SQL and History
PPT
Data mining techniques unit 1
PPT
Textmining Introduction
PPTX
Html ppt
An introduction to MongoDB
MongoDB and Indexes - MUG Denver - 20160329
Major issues in data mining
Database testing
Basic SQL and History
Data mining techniques unit 1
Textmining Introduction
Html ppt

What's hot (20)

PPTX
How to Achieve Scale with MongoDB
PPT
MySQL Transactions
PPTX
Indexing with MongoDB
PDF
NoSQL Databases
PPT
Lossless decomposition
PDF
Microservice Design Patterns.pdf
ODP
Knowledgebase vs Database
PDF
Introduction of Knowledge Graphs
PDF
Big Data: Big SQL and HBase
PPTX
In-Memory DataBase
PPTX
Relational Data Model Introduction
PDF
Apuntes de XSD
PPT
JavaScript - An Introduction
PPT
Fast querying indexing for performance (4)
PDF
MongoDB Schema Design (Richard Kreuter's Mongo Berlin preso)
PPTX
PPTX
introdution to SQL and SQL functions
PDF
HTML5: features with examples
PPT
Data independence
PDF
Developing for Node.JS with MySQL and NoSQL
How to Achieve Scale with MongoDB
MySQL Transactions
Indexing with MongoDB
NoSQL Databases
Lossless decomposition
Microservice Design Patterns.pdf
Knowledgebase vs Database
Introduction of Knowledge Graphs
Big Data: Big SQL and HBase
In-Memory DataBase
Relational Data Model Introduction
Apuntes de XSD
JavaScript - An Introduction
Fast querying indexing for performance (4)
MongoDB Schema Design (Richard Kreuter's Mongo Berlin preso)
introdution to SQL and SQL functions
HTML5: features with examples
Data independence
Developing for Node.JS with MySQL and NoSQL
Ad

Viewers also liked (16)

PPTX
Hash tables
PPTX
Hash table and heaps
PDF
Balanced Trees
PPT
Best for b trees
PPT
Hashing
PPT
B trees in Data Structure
PPT
Concept of hashing
PPTX
Trees (data structure)
PDF
Hashing and Hash Tables
PPT
Hashing PPT
PPTX
Hashing Techniques in Data Structures Part2
PPT
Hashing
PPT
17. Trees and Graphs
PPTX
Trees data structure
PPTX
Hashing Technique In Data Structures
Hash tables
Hash table and heaps
Balanced Trees
Best for b trees
Hashing
B trees in Data Structure
Concept of hashing
Trees (data structure)
Hashing and Hash Tables
Hashing PPT
Hashing Techniques in Data Structures Part2
Hashing
17. Trees and Graphs
Trees data structure
Hashing Technique In Data Structures
Ad

Recently uploaded (20)

PDF
Computing-Curriculum for Schools in Ghana
PPTX
Presentation on HIE in infants and its manifestations
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
master seminar digital applications in india
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Lesson notes of climatology university.
PPTX
Pharma ospi slides which help in ospi learning
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
GDM (1) (1).pptx small presentation for students
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Cell Structure & Organelles in detailed.
Computing-Curriculum for Schools in Ghana
Presentation on HIE in infants and its manifestations
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
master seminar digital applications in india
Abdominal Access Techniques with Prof. Dr. R K Mishra
102 student loan defaulters named and shamed – Is someone you know on the list?
Lesson notes of climatology university.
Pharma ospi slides which help in ospi learning
human mycosis Human fungal infections are called human mycosis..pptx
GDM (1) (1).pptx small presentation for students
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Microbial diseases, their pathogenesis and prophylaxis
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
O7-L3 Supply Chain Operations - ICLT Program
VCE English Exam - Section C Student Revision Booklet
Cell Structure & Organelles in detailed.

Intro to Hash tables

  • 1. The answer to your interview question is probably a hash table intro to hash tables Amy Hua
  • 2. Why hash tables? Great for quickly looking something up. Usually faster than arrays or most anything else. Big O Performance: O(1) to O(n)
  • 4. For example • Say you have an array of ~100 items – To look up item at position 3: • myarray[3] • O(1) • But how do you know that the item you’re looking for is in position 3?
  • 5. For example In a phone book, how do you know Jane Smith is on page 245? You probably used an index (last name: Smith) to find the page or range of pages Jane Smith would be located on.
  • 6. For example In a phone book, how do you know Jane Smith is on page 245? LookUpPageByLastName(“Smith”) = page 245 hash function
  • 7. If you know how to use a phone book, you know how to use a hash table Hash table: data structure for quickly looking things up. Given some key, we can apply a hash function to it to find the index in the array that we want to access. hash function: takes a key  returns an index in the array LookUpPageByLastName(“Smith”) = 245 hash function key index
  • 8. Example hash function Given keys keys: Lizzie McGuire Fiona Apple James Bond Index them by converting their names to integers Lizzie McGuire  13223 Fiona Apple  12342 James Bond  23523 Divide their numbers by a large enough number (modulo) Lizzie McGuire  3,223 % 1000 = 3 remainder 0.223 Fiona Apple  4,342 % 1000 = 4 remainder .342 James Bond  9,523 % 1000 = 9 remainder .523 Hash table: stores the key (used to find the index) along with the associated value we’re looking up.
  • 9. Collisions Sometimes, more than one name is on a given page: multiple elements can have the same index. In other words, collisions can occur in our hash table. How do you handle collisions?
  • 10. Collisions Well what do you do when you see multiple names on the same page in a phone book? You go through a linked list
  • 11. You now know how to use a phone book.. again
  • 12. Sources • http://www.cs.uregina.ca/Links/class- info/210/Hash/ References • http://www.youtube.com/watch?v=MfhjkfocRR0&li st=TLxupPRqcHJiw • http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/n otes/07-hashing.pdf • http://courses.csail.mit.edu/6.006/spring11/rec/rec 05.pdf