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

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

SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 

Último (20)

Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the life
 
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
“O BEIJO” EM ARTE .
“O BEIJO” EM ARTE                       .“O BEIJO” EM ARTE                       .
“O BEIJO” EM ARTE .
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 

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