Amazon Interview Question for Software Engineer in Tests






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

## In above example .. searching for key 'Words'
SearchKey(\%hash,'Words'); #SearchKey Funct Pass hashRef and key to be searched

sub SearchKey
{
        my $href = shift;
        my $string = shift;

        my $value = hashTraverse($href,$string);
        print "\n $string === $value\n\n";
}

sub hashTraverse
{
        my $href = shift;
        my $key = shift;
        my $value = $href->{$key};
        my $ref = ref($value);

        if($ref eq "" and defined $value and $value ne "")
        {
                return $value;
        }
        else
        {
                foreach my $tmp(keys %$href)
                {
                        if((ref($href->{$tmp}) eq 'HASH' or ref($href->{$tmp}) eq 'ARRAY') and $tmp eq $key)
                        {
                                print "\nValue reference ",$href->{$tmp},"\n";
                                print Dumper($href->{$tmp});
                                exit 0;
                        }
                        elsif(ref($href->{$tmp}) eq 'HASH')
                        {
                                my $gotit = hashTraverse($href->{$tmp},$key);
                                return $gotit;
                        }
                        elsif(ref($href->{$tmp}) eq 'ARRAY')
                        {
                                my $new = $href->{$tmp};
                                my $index = 0;
                                foreach my $element(@$new)
                                {
                                        my $type = ref($element);
                                        if($element eq $key)
                                        {
                                                print "\n Array Element $element \n\n";
                                                exit 0;
                                        }
                                        if($type eq 'HASH')
                                        {
                                                my $gotit = hashTraverse($href->{$tmp}->[$index],$key);
                                                return $gotit if($gotit ne "");
                                        }
                                        $index++;
                                }
                        }
                }
        }
}

- Abhishek May 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

seems like JSON format ... If we can find grammar for this we can evaluate the expression

- Anonymous May 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use Data::Dumper;

%h = (a=>0,b=>1,c=>[{'one'=>1,'two'=>2},{1=>'one',2=>'two'},{'number'=>'decimals'},{3 =>'alphas'},'sample']);
%hash = ('abc'=>[123,456,789],'pqr'=>345,'xyz'=>567,'opq'=>'word','lmn'=>\%h,'hello'=>{'zhang'=>'yixing','michael'=>'zhang'});

while($input = <STDIN>) {
chomp($input);
print search(\%hash,$input);
print "\n";
}

sub search {
my $dataRef = shift;
my $key = shift;
if(exists $dataRef->{$key}) {
return $dataRef->{$key};
}
foreach $temp (keys %$dataRef) {
if(ref($dataRef->{$temp}) eq 'HASH') {
$temp = search($dataRef->{$temp},$key);
if($temp) {
return $temp;
}else {
next;
}
}if(ref($dataRef->{$temp}) eq 'ARRAY') {
return searchArray($dataRef->{$temp},$key);
}
}
}

sub searchArray {
my $dataRef = shift;
my $key = shift;
foreach my $element (@$dataRef) {
if(ref($element) eq 'ARRAY') {
return searchArray($element, $key);
}if(ref($element) eq 'HASH') {
$temp = search($element,$key);
if($temp) {
return $temp;
}else {
next;
}
}
}
}

- Michael February 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use Data::Dumper;

%h = (a=>0,b=>1,c=>[{'one'=>1,'two'=>2},{1=>'one',2=>'two'},{'number'=>'decimals'},{3 =>'alphas'},'sample']);
%hash = ('abc'=>[123,456,789],'pqr'=>345,'xyz'=>567,'opq'=>'word','lmn'=>\%h,'hello'=>{'zhang'=>'yixing','michael'=>'zhang'});

while($input = <STDIN>) {
chomp($input);
print search(\%hash,$input);
print "\n";
}

sub search {
my $dataRef = shift;
my $key = shift;
if(exists $dataRef->{$key}) {
return $dataRef->{$key};
}
foreach $temp (keys %$dataRef) {
if(ref($dataRef->{$temp}) eq 'HASH') {
$temp = search($dataRef->{$temp},$key);
if($temp) {
return $temp;
}else {
next;
}
}if(ref($dataRef->{$temp}) eq 'ARRAY') {
return searchArray($dataRef->{$temp},$key);
}
}
}

sub searchArray {
my $dataRef = shift;
my $key = shift;
foreach my $element (@$dataRef) {
if(ref($element) eq 'ARRAY') {
return searchArray($element, $key);
}if(ref($element) eq 'HASH') {
$temp = search($element,$key);
if($temp) {
return $temp;
}else {
next;
}
}
}
}

- Michael February 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

##################################################
# Function IsArray                               #
# Input: reference                               #
# Output: 1 iff input is a reference to an array #
##################################################
sub IsArray{
   if(ref($_[0]) eq 'ARRAY'){
	   1;
   }else{
		0;
   }
}

##################################################
# Function IsHash                                #
# Input: reference                               #
# Output: 1 iff input is a reference to a Hash   #
##################################################
sub IsHash{
#print "DEBUG-------->",ref($_[0]),"\n";
   if(ref($_[0]) eq 'HASH'){
	   1;
   }else{
		0;
   }
}

##################################################
# Function Search                                #
# Input: nested hash , key                       #
# Output: If key exists in the hash at any level,#
#         output the value, otherwise undef      #
# Algorithm: Recursively go on the values of the #
#            input hash / array.                 #
##################################################
sub Search{
	my ($hashRef , $key ) = @_;

	# print "DEBUG2 ----> Seaching for $key\n";

	# Input is a hash - check for the key, and if not go through the other values
	if (IsHash($hashRef)) {
		if (exists($$hashRef{$key})) {
			return ($$hashRef{$key});
		}

		# Recursively go through the values of the hash
		for (values %$hashRef) {
			if (IsArray($_) or IsHash($_)) {
				my $temp = Search($_,$key);
				if (defined $temp) {
					return $temp
				}
			}
		}
	}

	if (IsArray($hashRef)) {
		# Recursively go through the values of the array
		for (@$hashRef) {
			if (IsArray($_) or IsHash($_)) {
				my $temp = Search($_,$key);
				if (defined $temp) {
					return $temp
				}
			}
		}
	}

	return undef;
}

- leofer October 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One recursive function:

sub search {
    my ($hashref, $search_key) = @_;
    my $ret_val = undef;
    return $ret_val if ref($hashref) ne 'HASH';
    while ( my ($key, $val) = each %$hashref) {
        if ($key eq $search_key) {
            return $val;
        } elsif (ref($val) eq 'HASH') {
            $ret_val = search($val, $search_key);
        } elsif (ref($val) eq 'ARRAY') {
            foreach my $h (grep {ref($_) eq 'HASH'} @$val) {
                $ret_val = search($h, $search_key);
            }
        }
    }
    return $ret_val;
}

- Anonymous February 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

# Recursion makes the solution easy
sub search_hash {
my $hash_ref = shift;
my $search_key = shift;
foreach my $key (keys %$hash_ref) {
if ($key eq $search_key) {
if (ref($hash_ref->{$key}) eq 'ARRAY') {
print Dumper($hash_ref->{$key});
return;
}
elsif (ref($hash_ref->{$key}) eq 'HASH') {
print Dumper($hash_ref->{$key});
return;
}
elsif (ref($hash_ref->{$key}) eq '') {
print $hash_ref->{$key};
return;
}
}
elsif (ref($hash_ref->{$key}) eq 'HASH') {
search_hash($hash_ref->{$key}, $search_key);
}
elsif (ref($hash_ref->{$key}) eq 'ARRAY') {
search_array($hash_ref->{$key}, $search_key);
}
}
}

sub search_array {
my $arr_ref = shift;
my $search_key = shift;
foreach my $elem (@$arr_ref) {
if (ref($elem) eq 'HASH') {
search_hash($elem, $search_key);
}
if (ref($elem) eq 'ARRAY') {
search_array($elem, $search_key);
}
}
}

- venkibs.eng August 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sub search {
	
	my $hash_ref = shift;
	my $srch_key = shift;
	
	foreach my $key ( keys(%{$hash_ref}) ) {
		
		if( $key eq $srch_key ) {
			print "\nFound! " . $hash_ref->{$key};
			return $hash_ref->{$key};
			
		} else {
			print "\nref $key == ". ref($hash_ref->{$key});
			if( ref($hash_ref->{$key}) =~ /HASH/ ) {
				
				return search($hash_ref->{$key},$srch_key);
				
			} elsif( ref($hash_ref->{$key}) =~ /ARRAY/ ) {
				
				my @array = @{$hash_ref->{$key}};
				foreach my $element ( @array) {
					
					if( ref($element) =~/HASH/ ) {
						my $val = search($element, $srch_key);
						return $val unless $val == '';
					}
				}
			}
		}			
	}

} # end sub search

- D October 31, 2013 | 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