Monday, December 24, 2012

Java Interview Questions

Collections -

Explain how HashMap works


HashMap - used array as an internal data structure, where each array position is treated as bucket and elements are stored in buckets using linked list.

Adding into HashMap
 1. Call hashcode method on the key and then use its own hashing function to get a hashcode (This is to make sure that its hashcode failry distribute bukets)
2. Use a mode operation on hashcode with size of array and find out bucket location.
3. Traverse all the elements in the bucket and check if key matches using equals method on keys
4. if key matches update the value else add new entry.

similar steps for delete and retrieve.

Perl how to find a exact word from all files under current directory (One level search only)

# Find out where all you see hello word in files
use warnings;
use strict;

my @files = <*.*>;

foreach my $file(@files) {
    if(-e -f $file) {
        open my $file_handler, '<, $file;
        while(<$file_handler>)  {
            if(/\A(hello)\z / ) {
                print $1;
            }
        }
       close $file_handle;
    }
}