Some logical interview questions & solutions 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.

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]
<?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)." 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);
\$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;

?>
[/php]

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

[php]
<?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!";
}
?>
[/php]

How to insert an element in linked list?

[php]
<?php
{
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 __toString()
{
\$str = \$this->value;
if(\$this->next != null)
{
\$str .= " » ";
\$str .= \$this->next;
}
return \$str;
}
}

echo \$head . "\n"; // output is "foo » bar » baz » mahi"
?>
[/php]

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

[php]
<?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>";
?>
[/php]