HomeWeb Scripts & Programming

Some logical interview questions & solutions

Some logical interview questions & solutions
Like Tweet Pin it Share Share Email

Below are some interview questions that usually asked during the technical interview if you are invited as a Sr. Developer.

 

These are very simple but it is always advice to read/learn and prepare yourself for interview.

 

These interview questions are just for checking your logic instead what you have done in your previous experience and how much strong you are in your programming language.

 

Hope these will help you a lot.

 

Questions are as below.

  1. How to get sum of diagonal elements of a symmetrical array using a single loop?
  2. How to check whether the string is palindrome or not?
  3. How to insert an element in linked list?
  4. Find the largest decreasing order array/series or sequence from a given array.

 

Try yourself once, if you need help scroll the page down 🙂

 

How to get sum of diagonal elements of a symmetrical array?
(Symmetrical array is an array that have same number of columns and rows)

<?php
$array = array(
				0=>array(4,5,6,2),
				1=>array(1,2,3,1),
				2=>array(2,2,2,5),
				3=>array(2,2,2,2)
		 );

echo "<pre>";
print_r($array);
echo "</pre>";

echo "<br />";
echo "The array have ".count($array)." rows.";
echo "<br />";
echo "The array have ".count($array[0])." columns";
echo "<br />";
echo "The array have ".(count($array,COUNT_RECURSIVE) - count($array))." elements";
echo "<br />";

$sum_diagonal_element = 0;
for($i=0,$j=0; $i < count($array); $i++){
	
	if($i==$j){
		$sum_diagonal_element += $array[$i][$j];
	}		
	$j++;
}
echo "<br />";
// SUM OF A DIAGONAL ELEMENTS
echo $sum_diagonal_element;


// -----
$sum_both_diagonal_element = 0;
$coloumns                  = count($array[0]);
$sum_common                = 0;

for($i=0,$j=0; $i < count($array); $i++){
	$coloumns--;
	if($i == $j){
		$sum_both_diagonal_element += $array[$i][$j];
		if($j != $coloumns){ // skip the common elements
		$sum_both_diagonal_element += $array[$i][$coloumns];
		}
	}
	$j++;
}
echo "<br />";
// SUM OF A BOTH DIAGONAL ELEMENTS
echo $sum_both_diagonal_element;

?>

 

How to check whether the string is palindrome or not?
(Palindrome means that it’s (string) reverse will remain same as it is)

<?php
$string    = "SCRIPTARTICLE-ELcITRATPIRCs";

$newstring = "";
for($i = strlen($string)-1; $i >= 0; $i--){
	
	$newstring .= $string[$i];
}

// It is better to change the case of string characters
if(strtolower($string) == strtolower($newstring)){
	echo "String is palindrome!";
}else{
	echo "String is not palindrome!";
}
?>

 

How to insert an element in linked list?

<?php
class LinkedObject
{
    var $value;
    var $prev;
    var $next;

    public function __construct($value, $prev = null, $next = null)
    {
        $this->value = $value;
        $this->prev  = $prev;
        $this->next  = $next;
    }

    public function append(LinkedObject $insertee)
    {
        $link = $this;
        while($link->next != null)
            $link = $link->next;

        $link->next = $insertee;
        $insertee->prev = $link;
    }

    public function __toString()
    {
        $str = $this->value;
        if($this->next != null)
        {
            $str .= " » ";
            $str .= $this->next;
        }
        return $str;
    }
}

$head = new LinkedObject("foo");
$head->append(new LinkedObject("bar"));
$head->append(new LinkedObject("baz"));
$head->append(new LinkedObject("mahi"));
echo $head . "\n"; // output is "foo » bar » baz » mahi"
?>

 

Find the largest decreasing order array/series or sequence from a given array.

<?php
$array = array(10,9,8,5,17,16,15,25,24,23,22,21,20,19,14,13,12,34,33,90);

$mycount 		= 0;
$arr_desc_order = array();

for($i=0; $i < count($array); $i++){

	if($i == 0){
			$arr_desc_order[$mycount][] = $array[$i];
	}else{
		if($array[$i-1] < $array[$i]){
			$mycount++;
		}
		$arr_desc_order[$mycount][]     = $array[$i];
	}

}

// Find the array with maximun number of elements
$arr_elemt = $count= 0;
foreach($arr_desc_order as $arr){
	if(count($arr) > $arr_elemt){
	$arr_elemt = $count;
	}
	$count++;
}

echo "<pre>";
print_r($arr_desc_order[$arr_elemt]);
echo "</pre>";
?>

 

Hope the above will help you to crack the interview.
Best of luck!!

Comments (3)

Comments are closed.