SlideShare uma empresa Scribd logo
1 de 12
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

Mais conteúdo relacionado

Mais procurados

Tutorial for using SQL in Microsoft Access
Tutorial for using SQL in Microsoft AccessTutorial for using SQL in Microsoft Access
Tutorial for using SQL in Microsoft Access
mcclellm
 
Search engine
Search engine Search engine
Search engine
28student
 
Database management system
Database management systemDatabase management system
Database management system
Altacit Global
 
Database management system presentation
Database management system presentationDatabase management system presentation
Database management system presentation
sameerraaj
 
Enhanced E-R diagram
Enhanced E-R diagramEnhanced E-R diagram
Enhanced E-R diagram
Mayank Jain
 

Mais procurados (20)

Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005
 
Record Deduplication and Record Linkage
Record Deduplication and  Record LinkageRecord Deduplication and  Record Linkage
Record Deduplication and Record Linkage
 
Tutorial for using SQL in Microsoft Access
Tutorial for using SQL in Microsoft AccessTutorial for using SQL in Microsoft Access
Tutorial for using SQL in Microsoft Access
 
Google Dorks
Google DorksGoogle Dorks
Google Dorks
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 
Cross site scripting
Cross site scriptingCross site scripting
Cross site scripting
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
MYSQL.ppt
MYSQL.pptMYSQL.ppt
MYSQL.ppt
 
HTML5: features with examples
HTML5: features with examplesHTML5: features with examples
HTML5: features with examples
 
Search engine
Search engine Search engine
Search engine
 
Database security
Database securityDatabase security
Database security
 
XML Sitemap and Robots.TXT Guide for SEO Beginners
XML Sitemap and Robots.TXT Guide for SEO BeginnersXML Sitemap and Robots.TXT Guide for SEO Beginners
XML Sitemap and Robots.TXT Guide for SEO Beginners
 
Java script arrays
Java script arraysJava script arrays
Java script arrays
 
Hashing data
Hashing dataHashing data
Hashing data
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
Database management system
Database management systemDatabase management system
Database management system
 
Web application architecture
Web application architectureWeb application architecture
Web application architecture
 
Database management system presentation
Database management system presentationDatabase management system presentation
Database management system presentation
 
Enhanced E-R diagram
Enhanced E-R diagramEnhanced E-R diagram
Enhanced E-R diagram
 

Destaque (16)

Hash tables
Hash tablesHash tables
Hash tables
 
Ie
IeIe
Ie
 
Hash table and heaps
Hash table and heapsHash table and heaps
Hash table and heaps
 
Balanced Trees
Balanced TreesBalanced Trees
Balanced Trees
 
Best for b trees
Best for b treesBest for b trees
Best for b trees
 
Hashing
HashingHashing
Hashing
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
 
Concept of hashing
Concept of hashingConcept of hashing
Concept of hashing
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Hashing and Hash Tables
Hashing and Hash TablesHashing and Hash Tables
Hashing and Hash Tables
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
 
Hashing Techniques in Data Structures Part2
Hashing Techniques in Data Structures Part2Hashing Techniques in Data Structures Part2
Hashing Techniques in Data Structures Part2
 
Hashing
HashingHashing
Hashing
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Último (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 

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