Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
1
of 1 vote

Hashmaps store things by key, so what you're asking for doesn't make much sense. Every stored value must have an associated key.

- eugene.yarovoi October 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think we can iterate the entire map and keep replacing key with value.. and set value as null..

- Anonymous October 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Eugene: This was asked in an interview. I know it doesn't make sense to remove keys alone, but may be interviewer wants to check some thing else here like how to iterate and remove entries from a map or something like that !!

Thanks for the suggestion !!

- SK October 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

using ketset() you obtain the set of key in the hashmap. But making all the keys to null is not possible. Hashmap can have only one null key in whole of its hashmap.

- Surendar November 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use Hasmap.keySet() to get all the keys populated in a set and than iterate through it and remove the odd entries ...

- rahul chaudhary October 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Will it not throw concurrentModificationException ?- trying to remove while iterating.

- Anonymous November 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, every time should update, keyset and iterator

- Andi November 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, it won't throw concurrentModification exception if you traverse using iterator, and remove using iterator.remove method.

- SK November 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if

- rahul September 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can do this :Hasmap.keySet() to get all the keys populated in a set and than iterate through it and remove the odd entries.

But while deleting through iterator, it will delete value also. I mean that entryset will be deleted.

- @ rahul chaudhary September 25, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More