hashmap interview questions
Top hashmap frequently asked interview questions
That is pretty easy with a plain hash like
{:a => "a", :b => "b"}
which would translate into
"a=a&b=b"
but what do you do with something more complex like
{:a => "a", :b => ["c", "d", "e"]}
which should translate in
"a=a&b[0]=c&b[1]=d&b[2]=e"
or even worse, with something like:
{:a => "a", :b => [{:c => "c", :d => "d"}, {:e => "e", :f => "f"}]
Thanks for the much appreciated help with that!
Source: (StackOverflow)
If I have the value "foo"
, and a HashMap<String> ftw
for which ftw.containsValue("foo")
returns true
, how can I get the corresponding key? Do I have to loop through the hashmap? What is the best way to do that?
Source: (StackOverflow)
Suppose we have a HashMap<String, Integer>
in Java.
How do I update (increment) the integer-value of the string-key for each existence of the string I find?
One could remove and reenter the pair, but overhead would be a concern.
Another way would be to just put the new pair and the old one would be replaced.
In the latter case, what happens if there is a hashcode collision with a new key I am trying to insert? The correct behavior for a hashtable would be to assign a different place for it, or make a list out of it in the current bucket.
Source: (StackOverflow)
HashMap has two important properties: size and load factor. I went through the Java documentation and it says 0.75f is the initial load factor. But I can't find the actual use of it. Can someone describe what are the different scenarios where we need to set load factor and what are some sample ideal values for different cases?
Source: (StackOverflow)
How to convert a ruby hash object to JSON? So I am trying this example below & it doesn't work?
I was looking at the RubyDoc and obviously Hash
object doesn't have a to_json
method. But I am reading on blogs that Rails supports active_record.to_json
and also supports hash#to_json
. I can understand ActiveRecord
is a Rails object, but Hash
is not native to Rails, it's a pure Ruby object. So in Rails you can do a hash.to_json
, but not in pure Ruby??
car = {:make => "bmw", :year => "2003"}
car.to_json
Source: (StackOverflow)
I'm requesting data from a server which returns data in the JSON format. Casting a HashMap into JSON when making the request wasn't hard at all but the other way seems to be a little tricky. The JSON response looks like this:
{
"header" : {
"alerts" : [
{
"AlertID" : "2",
"TSExpires" : null,
"Target" : "1",
"Text" : "woot",
"Type" : "1"
},
{
"AlertID" : "3",
"TSExpires" : null,
"Target" : "1",
"Text" : "woot",
"Type" : "1"
}
],
"session" : "0bc8d0835f93ac3ebbf11560b2c5be9a"
},
"result" : "4be26bc400d3c"
}
What way would be easiest to access this data? I'm using the GSON module.
Source: (StackOverflow)
Is checking for key existence in HashMap always necessary?
I have a HashMap with say a 1000 entries and I am looking at improving the efficiency.
If the HashMap is being accessed very frequently, then checking for the key existence at every access will lead to a large overhead. Instead if the key is not present and hence an exception occurs, I can catch the exception. (when I know that this will happen rarely). This will reduce accesses to the HashMap by half.
This might not be a good programming practice, but it will help me reduce the number of accesses. Or am I missing something here?
[Update] I do not have null values in the HashMap.
Source: (StackOverflow)
How can I loop through a HashMap
in JSP?
<%
HashMap<String, String> countries = MainUtils.getCountries(l);
%>
<select name="country">
<%
// Here I need to loop through countries.
%>
</select>
Source: (StackOverflow)
A lot of times people use symbols as keys in a Ruby hash.
What's the advantage over using a string?
E.g.:
hash[:name]
vs.
hash['name']
Source: (StackOverflow)
How can you create the JavaScript/JQuery equivalent of this Java code:
Map map = new HashMap(); //Doesn't not have to be a hash map, any key/value map is fine
map.put(myKey1, myObj1);
map.put(myKey2, myObj2); //Repeat n times
function Object get(k) {
return map.get(k);
}
Source: (StackOverflow)
I'll admit that I'm a bit of a ruby newbie (writing rake scripts, now). In most languages, copy constructors are easy to find. Half an hour of searching didn't find it in ruby. I want to create a copy of the hash so that I can modify it without affecting the original instance.
Some expected methods that don't work as intended:
h0 = { "John"=>"Adams","Thomas"=>"Jefferson","Johny"=>"Appleseed"}
h1=Hash.new(h0)
h2=h1.to_hash
In the meantime, I've resorted to this inelegant workaround
def copyhash(inputhash)
h = Hash.new
inputhash.each do |pair|
h.store(pair[0], pair[1])
end
return h
end
Source: (StackOverflow)
As per my understanding I think:
- Its perfectly legal for two objects to have same hashcode.
- If two objects are equal (using equals ) then they have same hashcode.
- If two object are not equal then they cannot have same hashcode
Am I correct?
Now if am correct, I have following question:
HashMap
internally uses hashcode of the object. Then if two objects can have same hashcode, then how can the HashMap
track which key it uses?
Can someone explain how HashMap internally uses the hashcode of the object?
Source: (StackOverflow)