Hash Function
A hash function takes a data like string and outputs a hash, a fixed-size string or number.
E.g MD5 hash for a string “alok” is bad220c335d0c1f53548f6acdb17e265
import hashlib# initializing stringstr2hash = "alok"result = hashlib.md5(str2hash.encode())print(result.hexdigest())bad220c335d0c1f53548f6acdb17e265
Notes
- A given file will always have same hash but we can’t go back from hash to original value
- Sometime multiple string having same hash value, which is called hash collision.
Use cases of hash map
- Unordered map or dictionary : Creating a look up table in programming language.
- Preventing man-in-the-middle attack
Q. You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). You need to do this in place.
Question asked in Amazon Google Facebook Design
<!DOCTYPE html>
<html>
<head>
<style>
.container {width: 400px;height: 400px;position: relative;background-color: yellow;margin: auto;margin-top: 100px;}.animate {width: 50px;height: 50px;background-color: red;position: absolute;}.animate2 {width: 50px;height: 50px;background-color: red;top: 0;right: 0;position: absolute;}button {position: relative;top: 20px;margin-left: 700px;}</style></head><body><!-- <h1 id="demo"></h1> --><div class=container><div class="animate"></div><div class="animate2"></div></div><button onclick="call()">Button</button><script>function call(){const animate = document.querySelector('.animate')const animate2 = document.querySelector('.animate2')var pos=0;var timeInterval=setInterval(function(){pos++;if(pos==350){clearInterval(timeInterval)
}animate.style.top=`${pos}px`;animate.style.left=`${pos}px`;animate2.style.top=`${pos}px`;animate2.style.right=`${pos}px`;},5)}</script></body></html>