Programming Tips 101 - The Right Way To Access Hash table
Vložit
- čas přidán 7. 09. 2024
- In this video, I am discussing what most programmers do while accessing the hash map or hash table which they don't need to. What they do is that they just waste CPU cycles and consume more power and they can very easily change the code to be 2x faster and consuming half power.
This is a new programming tips series I am starting. Let me know in the comments do you find such videos useful or not.
Please follow me on / think.software.community if you like to get notified about new course chapters getting added or when we will start another round of mock interviews and you want to participate in mock interviews or any other updates. I will also take your suggestions there about the course and the channel.
Check out our following articles:
- How to Ace Object-Oriented Design Interviews: / how-to-ace-object-orie...
- Elevator System Design - A tricky technical interview question: / elevator-system-design...
- System Design of URL Shortening Service like TinyURL: / tinyurl-design-from-th...
- File Sharing Service Like Dropbox Or Google Drive - How To Tackle System Design Interview: / how-to-tackle-system-d...
- Design Twitter - Microservices Architecture of Twitter Service: / design-twitter-microse...
FAANG #Facebook #Google #Amazon #Apple #Microsoft #Uber #Netflix #Oracle #Lyft #SystemDesign #Interview #ComputerProgramming #ProgrammingTips101
Please let me know in the comments below, do you find this video useful and do you like me to make more such videos? Also please do like the video on the CZcams website if you find this video useful. Thanks.
A very basic but a good point. Generally people use the 2x approach. Its good that you pointed out
Thanks for the comment
I use -> Value getValue {
return config.getOrDefault(paramKey, someDefaultValue)
}
Thanks for the comment
Thanks for the videos, especially about System Design.
At 5:50 you said that unordered_map throws an exception if there is no such key - this is not true.
In this case hash map will create a new entry with constructing a default obj and return it.
Moreover, STL does not throw exceptions if developer uses [] to get to something (vector, map, unordered_map) it does only if you call method at(). It is done for the sake of performance. So developer should be wise and pick the right way to access to the element in the container.
This is such an unnoticeable but big mistake we make while coding. Thanks! 🙏 keep making such amazing videos!
Thanks for the comment 🙂
Keep making like this, loving ur videos
Thanks for the comment 🙂
The trick to avoid the containsKey() - that was very informative. Thank you for sharing the info.
Looking forward to more videos in this channel.
I also have the following questions:
1) The method getData() is a synchronized method. So, shouldn’t we use a ConcurrentHashMap here, I know the major point here was how to avoid accessing the map two times to check if key is there or not, but still checking.
2) In the second example, in the method getSomeParameterFromConfig(), the map.getOrDefault() would be more appropriate, right?
Thanks for the comment. Regarding your questions 1) using concurrent map depends on the use case and this video was not targeting that. 2) yes you can use that in case of Java.
When are you uploading the next video?
Coming soon :)
A very basic but useful video. Thank you. I have a career question: For most of the roles in tech companies there are some years of experience as criteria mentioned in the job post. For example: for SDE 3 they need 8 years of experience, etc. Is it mandatory to have those many years of experience as mentioned. What if someone has less experience but still can clear the SDE 3 bar. Will the big tech companies consider such candidate?
The company will not offer you SDE 3 simply because you have clear the SDE 3 interview bar in an interview. The company will only offer you SDE 3 if there is a need. E.g. you have enough experience that would require them to justify you as SDE 3 and if they downlevel you then their own management can question the hiring manager. Or you are already at SDE 3 level in your current company and you may not join them as SDE 2 because you tell them during negotiation that you will not take downlevel. Or you have competing offers where other companies are offering you SDE 3 or higher pay which that company can only offer you at SDE 3 level. So it then all boils down to your negotiation skills (after your interview performance) and your circumstances.
@@ThinkSoftware I got your point. Thank you :)
As a Java programmer, I would probably use computeIfAbsent()
Yes. The point is to avoid two lookups if possible. Were you doing two lookups till now or one lookup?
Hey, there is no put(obj) method on the map. You will have to put(Key, obj). Please correct it on the first example.
For the second code, You can use getDefault method on Hashmap interface, which takes a key and a default value. If Key does not exists, it directly returns the default value; which is what you are trying to do here essentially.
Just a mistake. For getDefault() it is present in Java but not in C++ and I was giving a general example.
Thanks for the tip.
thanks for the comment :)
For getData, why does the code also include functionality of constructing the object and putting it into the cache?? That was surprising to me.
It depends on the functional requirement of the function. Requirement could be getData() always return data, either from local cache or if not in cache then go to DB to get data. However this was not the point of the video.
Think Software That makes sense
What are your thoughts about the engineering manager career path? In contrast to an individual contributor?
They require different jobs, skill-sets and mindset. Some people enjoy doing it and some not. I personally don't like some aspects of this job like performance evaluation, putting people on PIP or layoffs etc.
this is good
Thanks for the comment 🙂
good to know this! but i think this code is not thread-safe ;) that's another problem
Thanks for the comment 🙂. Yes it is not but that was not point of this video.
Hi, I like to learn why it is not thread safe. Can you give some explanation or provide some resource where I can look it up.
how to handle a case when value of a key is NULL/None
Better to use a sentinal value in that case.
1 / 10 :D
Thanks for the comment :)