Common Programming Interview questions and Answers

No, any technical interview can be completed without logical questions based on simple programming techniques. When you are going for a Programmer profile, you must know the simple logic applied to make complex programs. Here is the collection of such important logical questions asked in an interview.These collection includes reverse string,factorial,star printing pattern,swap,leap year,prime number,odd-even number,palindrome etc.

 

Write PHP script to reverse string without using any PHP function.


	function reverseString($string){
		$i=0;
		$output_string='';
		while($string[$i]){
			$output_string=$string[$i].$output_string;
			$i++;
		}
		return $output;
	}
	$my_string="this is a string to be reversed";
	$output=reverseString($my_string);

 

Write a program to find Factorial Of a number without using recursion.


function findFactorial($n){
	$factorial = 1;
	if($n<=1){
		return $factorial;
	}else if($n>1){
		$i=0;
		while($n-$i){
			$factorial = $factorial*($n-$i);
			$i++;
		}
	}

	return $factorial;

}
echo findFactorial(10);

 

Write a program to find Factorial of a number using recursion.


function findFactorialRec($number){
	if($number <= 1){    
        return 1;   
    }   
    else{   
        return $number * findFactorialRec($number - 1);   
    }   
}
echo findFactorialRec(10);

 

Write a program to print the following Pattern

*
*  *
*  *  *
*  *  *  *
*  *  *  *  *


$n=5;
for ($i=1;$i<=$n;$i++){
	for ($j=0; $j < $i ; $j++) { 
		echo "# ";
	}
	echo "<br>";
}
echo "<br>";

 

Write a program to print the following Pattern.

                                 *
                             *  *
                          *  *  *
                      *  *  *  *
                   *  *  *  *  *


for ($i=1;$i<=$n;$i++){
	for ($j=$n-$i; $j > 0 ; $j--) { 
		echo "+";
	}
	for ($k=0; $k < $i ; $k++) { 
		echo "*";
	}
	echo "<br>";
}
echo "<br>";

 

Write a program to print the following Pattern.

                                  *
                               * * *
                            * * * * *
                         * * * * * * *
                      * * * * * * * * *


$n=5;
for ($i=0; $i <$n-1 ; $i++) { 
	for($j=$n-$i;$j>0;$j--){
		echo "+";
	}
	for($k=1;$k<=(2*$i)+1;$k++){
		echo "*";
	}
	echo "<br>";
}
echo "<br>";

 

Write a function to check if a string is a palindrome or not.


function checkPalindrome($string){
	$length=strlen($string);
	if($length>1){
		$mid = floor($length/2);
		$pal=true;
		for ($i=0; $i <=$mid; $i++) { 
			if($string[$i]!=$string[$length-($i+1)]){
				$pal=false;
				break;
			}
		}
		if($pal==false){
			return "NOT A PALINDROM";
		}else{
			return "PALINDROM";
		}
	}
}

$string ="ABCDEfEDCBA";
echo checkPalindrome($string);

 

Write a program to check whether a given number is prime or not:


function checkPrime($number){
	$check=0;
	for($i=2;$i<=($number/2);$i++)
	{
		if($number%$i==0){
			$check++;
			if($check==1){
			 	break ;
			}
		}
	}
	if($check==0){
		echo "$number is a Prime Number";
	}else{
		echo "$number is not a Prime Number";
	}
}

 

Write a function to test a given year is Leap Year:


function checkLeapYear($year){
	if($year%4==0){
		echo "$year is a leap year";
	}else{
   		echo "$year is not a leap year";
 	}
}

 

Write a program to swap two numbers without using any temporary variable.


function swapNumbers($a,$b){
	$a=$a+$b;
	$b=$a-$b;
	$a=$a-$b;
	echo "Now A is $a and B is $b";
}

 

Write a program to check that a given number is odd or even.


function checkOddEven($number){ 
	if($number % 2 == 0){ 
		echo "$number is EVEN"; 
	} 
	else{ 
		echo "$number is ODD"; 
	} 
} 

 

Dhananjay

Dhananjay

I’m Dhananjay — a free-spirit with a passion for technologies and blogging.

View all posts by Dhananjay →

One thought on “Common Programming Interview questions and Answers

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.