Have positive attitude in an interview-Interesting Questions[Funny]

think out of the box

These are the few questions asked in HR interview (some of them already known to us)!! The answers are really stunning and inspiring. Thinking out of box! 

A must read…
Question 1: You are driving along in your car on a wild, stormy night, it's raining heavily, when suddenly you pass by a bus stop, and you see three people waiting for a bus: 

  • An old lady who looks as if she is about to die. 
  • An old friend who once saved your life. 
  • The perfect partner you have been dreaming about. 

Which one would you choose to offer a ride to, knowing very well that there could only be one passenger in your car?

Answer:
This is a moral/ethical dilemma that was once actually used as part of a job application. 

* You could pick up the old lady, because she is going to die, and thus you should save her first;
* or you could take the old friend because he once saved your life, and this would be the perfect chance to  pay him back.
* However, you may never be able to find your perfect mate again. 

The candidate who was hired (out of 200 applicants) had no trouble coming up with his answer. Guess what was his answer?
He simply answered: 
"I would give the car keys to my Old friend and let him take the lady to the hospital. I would stay behind and wait for the bus with the partner of my dreams." 

Sometimes, we gain more if we are able to give up our stubborn thought limitations. Never forget to "Think Outside the Box."

Question 2: What will you do if I run away with your sister?"
Answer: The candidate who was selected answered " I will not get a better match for my sister than you sir"

Question 3: Interviewer (to a student girl candidate) – What is one morning you woke up & found that you were pregnant.
Girl – I will be very excited and take an off, to celebrate with my husband.
Normally an unmarried girl will be shocked to hear this, but she managed it well. Why I should think it in the wrong way, she said later when asked

Question 4: Interviewer: He ordered a cup of coffee for the candidate.
Coffee arrived kept before the candidate, then he asked what is before you? 
Answer:Candidate: Instantly replied "Tea" 
He got selected. 
You know how and why did he say "TEA" when he knows very well that coffee was kept before. 
(Answer: The question was "What is before you (U – alphabet) 
Reply was "TEA" ( T – alphabet) 
Alphabet "T" was before Alphabet "U"

Question 5: Where Lord Rama would have celebrated his "First Diwali"?
Answer: People will start thinking of Ayodya, Mitila [Janaki's place], Lanka etc… 
But the logic is, Diwali was a celebrated as a mark of Lord Krishna Killing Narakasura.
In Dusavataar, Krishnavathaar comes after Raamavathaar.
So, Lord Rama would not have celebrated the Diwali At all!

Question 6: The interviewer asked to the candidate "This is your last question of the interview. 
Please tell me the exact position of the center of this table where u have kept your files." 
Answer
Candidate confidently put one of his finger at some point at the table and told that this was the central point at the table.
Interviewer asked how did u get to know that this being the central point of this table,
then he answers quickly that sir u r not likely to ask any more question, as it was the last question that u promised to ask….. 
And hence, he was selected as because of his quick-wittedness. ……..

Question 7: A man and his son are in a car crash. The father is killed and the child is taken to hospital gravely injured. When he gets there, the surgeon says, 'I can't operate on this boy – for he is my son!!!' How can this possibly be?
Answer: The surgeon cannot operate on her own son; she is his mother.

Question 8: There are six eggs in the basket. Six people each take one of the eggs. How can it be that one egg is left in the basket?
Answer: The last person took the basket with the last egg still inside.

Question 9: How could a baby fall out of a twenty-story building onto the ground and live?
Answer: The baby fell out of a ground floor window.

Question 10: Acting on an anonymous phone call, the police raid a house to arrest a suspected murderer. They don't know what he looks like but they know his name is John and that he is inside the house. The police bust in on a doctor, an engineer, a lawyer and a fireman all playing poker. Without hesitation or communication of any kind, they immediately arrest the fireman. How do they know they've got their man?
Answer: The fireman is the only man in the room. The rest of the poker players are women.
 

Never forget to "Think Outside the Box." 

Got this in internet, thought its good to share with you readers.
Image Source: teluguone.com

Sorting an array and hash elements in Perl

Sorting in PerlSorting an array and hash elements in Perl
This is the answer to Ques# 17 (a) and 23  under Perl Basics in Perl Interview Questions
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.
In Perl we have sort function which sorts a list alphabetically by default. But there is not the end. We need to sort:

  • an array numerically or
  • case insensitive strings or
  • case sensitive strings
  • hash contents by keys or
  • hash contents by values or
  • reverse of all above said points
 
How sorting  works in Perl
Sort subroutine has three syntaxes and last one is the most used syntax.
  • sort SUBNAME LIST
  • sort BLOCK LIST
  • sort LIST
In list context, it sorts the LIST and returns the sorted list value. In scalar context, the behavior of sort() is undefined.
If SUBNAME or BLOCK is omitted, sorts in standard string comparison order.

Standard string comparison means based on ASCII value of those characters. Like @arr = qw (Call all). In this case it will be sorted as Call all which was not expected. So to make it work properly we use case-insensitive sort.

 If SUBNAME is specified, it gives the name of a subroutine that returns an integer less than, equal to, or greater than 0 , depending on how the elements of the list are to be ordered. (The <=> and cmp operators are extremely useful in such routines.)

Note: The values to be compared are always passed by reference and should not be modified . $a and $b are global variable and should not be declared as lexical variables.

sort() returns aliases into the original list like grep, map etc  which should be  usually avoided for better readability.
As sorting always does string sorting, so to do numeric sorting we need to use a special syntax which a sort {$a ó $b} LIST. We will see these conditions using Perl codes.
 
How reverse sorting works
Systax to use reverse sort is reverse LIST. It works on sorted LIST usually. But in scalar context, it concatenates the elements of LIST and returns a string value with all characters in the opposite order.
In scalar context if argument is not passed it will reverse the value of $_

Ex:  

[perl]$_ = “dlrow ,olleH”;

print scalar reverse;  #in this case print reverse would not works because it expects a LIST  
[/perl]

How <=> and cmp work?
These are actually binary equality operator. Binary operator usually gives (0 or 1) or (true or false)  but these gives three values based on the comparison result.
Binary  “<=>” returns -1, 0, or 1 depending on whether the left argument is numerically less than, equal to, or greater than the right argument.
Binary “cmp” returns -1, 0, or 1 depending on whether the left argument is stringwise less than, equal to, or greater than the right argument.
Never mix string and numeric values in LIST else sorting result will be fearsome ðŸ™

Try this out:
[perl]
my @arr1 = qw(1 two 3 0 4  Two 5 six 7 8 9 ten);
my @arr2 = sort {$a cmp $b} @arr1;
print “\n@arr2\n”;
[/perl]
Let go through the codes for different scenarios:
Example 1: Sorting  an array of strings (case-sensitive and case-insensitive examples)
[perl]   
#!/usr/bin/perl
    use strict;
    use warnings;
 
    my @strarr = qw(two Two six Six alien Coders Alien coderS);
    my @sorted = sort {$a cmp $b} @strarr; # same result as of sort @strarr
    my @sortedcase  = sort { uc $a cmp uc $b } @strarr; #case-insensitivie
    print “\n@sorted\n@sortedcase\n”;
 [/perl]
Output:
 
Alien Coders Six Two alien coderS six two
alien Alien Coders coderS six Six two Two
 
Note: try to always use case insensitive for better string sorting results.
 
Example 2: Sorting an array of numbers
The Perl sort function sorts by strings instead of by numbers. If you do it in general way it would fetch unexpected result.
   [perl] #!/usr/bin/perl
    use strict;
    use warnings;
 
    my @numbers = (23, 1, 22, 7, 109, 9, 65, 3, 01, 001);
 
    my @sorted_numbers = sort @numbers;
    print “@sorted_numbers\n”;
[/perl]
The output you would see would be:
    001 01 1 109 22 23 3 65 7 9

To sort numerically, declare your own sort block and use the binary equality operator i.e. flying saucer operator <=>:
  [perl] 
   #!/usr/bin/perl
    use strict;
    use warnings;
 
    my @numbers = (23, 1, 22, 7, 109, 9, 65, 3, 01, 001);
 
    my @sorted_numbers = sort {$a <=> $b} @numbers;
    print “@sorted_numbers\n”;
[/perl]
The output would now be:
    1 01 001  3 7 9 22 23 65 109
Note that $a and $b do not need to be declared, even with use strict on, because they are special sorting variables.

Example 3: Sorting array backwards (for string and numbers)
To sort backwards you need to declare your own sort block, and simply put $b before $a. or use reverse keyword after simple sort.
For example, the standard sort is as follows:
 [perl]   #!/usr/bin/perl
    use strict;
    use warnings;
 
    my @strings = qw(Jassi Alien Coders);
 
    my @sorted_strings = sort @strings;
    print “@sorted_strings\n”;
[/perl]
The output would be:
    Alien Coders Jassi

To do the same, but in reverse order:
[perl] 
  #!/usr/bin/perl
    use strict;
    use warnings;
 
    my @strings = qw(Jassi Alien Coders);
 
    my @sorted_strings = sort {$b cmp $a} @strings; # or reverse sort @strings
    print “@sorted_strings\n”;
[/perl]
The output is:
Jassi Coders Alien

And for numbers:
[perl] 
  #!/usr/bin/perl
    use strict;
    use warnings;
 
    my @numbers = (23, 1, 22, 7, 109, 9, 65, 3);
 
    my @sorted_numbers = sort {$b <=> $a} @numbers; # or reverse sort {$aó $b} @numbers
    print “@sorted_numbers\n”;
[/perl]
The output is:
    109 65 23 22 9 7 3 1
This was all about sorting array elements alphabetically or numerically. Now we will see how sorting works on hash elements.

Example 4: Sorting hashes by keys
You can use sort to order hashes. For example, if you had a hash as follows:
Suppose we want to display the members for each community sorted alphabetically or say by keys, then this code will do so:
[perl] 
  #!/usr/bin/perl
    use strict;
    use warnings;
 
    my %members = (
        C => 1,
        Java => 7,
        Perl => 12,
        Linux => 3,
        Hacking => 8,
    );
foreach my $language (sort keys %members) {
        print $language . “: ” . $members{$language} . “\n”;
    }
[/perl]
Output:
    C: 1
    Hacking: 8
    Java: 7
    Linux: 3
    Perl: 12

If you want to sort the same hash by the values (i.e. the users beside each programming language), you could do the following:
[perl]
    #!/usr/bin/perl
    use strict;
    use warnings;
 
    my %members = (
        C => 1,
        Java => 7,
        Perl => 12,
        Linux => 3,
        Hacking => 8,
    );
    # Using <=> instead of cmp because of the numbers
    foreach my $language (sort {$members{$a} <=> $members{$b}} keys %members){
                print $language . “: ” . $members{$language} . “\n”;
}
 [/perl]

Output:
    C: 1
    Linux: 3
    Java: 7
    Hacking: 8
    Perl: 12

Example: 5 Sorting complex data structures
We can also use sort function to sort complex data structures. For example, suppose we have an array of hashes (anonymous hashes) like:
[perl] 
  my @aliens = (
        { name => ‘Jassi’, age => 28},
        { name => ‘Somnath’, age => 27},
        { name => ‘Ritesh’, age => 24},
        { name => ‘Santosh’, age => 29},
        { name => ‘Ranjan’, age => 26},
        { name => ‘Kaushik’, age => 25},
    );
[/perl]
And we wish to display the data about the people by name, in alphabetical order, we could do the following:
[perl] 
  #!/usr/bin/perl
    use strict;
    use warnings;
 
    my @aliens = (
        { name => ‘Jassi’, age => 28},
        { name => ‘Somnath’, age => 27},
        { name => ‘Ritesh’, age => 24},
        { name => ‘Santosh’, age => 29},
        { name => ‘Ranjan’, age => 26},
        { name => ‘Kaushik’, age => 25},
    );
 
    foreach my $person (sort {$a->{name} cmp $b->{name}} @aliens) {
        print $person->{name} . ” is ” . $person->{age} . “\n”;
    }
[/perl]
The output is:
    Jassi is 28
    Kaushik is 25
    Ranjan is 26
    Ritesh is 24
    Santosh is 29
    Somnath is 27

Sorting the same hash by age and using a subroutine (inline function)
Rather than writing the code inline, you can also pass in a subroutine name. The subroutine needs to return an integer less than, equal to, or greater than 0. Do not modify the $a and $b variables as they are passed in by reference, and modifying them will probably confuse your sorting.
[perl]  
#!/usr/bin/perl
    use strict;
    use warnings;
 
    my @aliens = (
        { name => ‘Jassi’, age => 28},
        { name => ‘Somnath’, age => 27},
        { name => ‘Ritesh’, age => 24},
        { name => ‘Santosh’, age => 29},
        { name => ‘Ranjan’, age => 26},
        { name => ‘Kaushik’, age => 25},
    );
 
    foreach my $person (sort agecomp @aliens) {
# it just replaced {$a->{age} <=> $b->{age}} by agecomp inline function
        print $person->{name} . ” is ” . $person->{age} . ” years old\n”;
    }
 
    sub agecomp {
        $a->{age} <=> $b->{age};
    }
[/perl]
The output would be:
    Ritesh is 24 years old
    Kaushik is 25 years old
    Ranjan is 26 years old
    Somnath is 27 years old
    Jassi is 28 years old
    Santosh is 29 years old

To find out more on sort function,  run the command on Linux box:
    perldoc -f sort