Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Most Asked Coding Questions In Placements

Top Most Asked Coding Questions in Placements

Here you will get most asked coding Questions in placements.

Coding Questions are important in both written as well as Interview Round. In Interview you will have to answer the coding questions whether you are a freshers or an experienced candidtae. 

Questions given on this page will help you in preparing for Coding questions in paper. Below You will find some of the most important codes in languages like C, C++ and Java and Python.These codes are very important since these will help you clear your basic concepts in various languages.

Click on the button below to solve top 100 codes asked in placement exams.

most asked coding questions in placements

This page will provide you information about Most Asked Coding Questions in Placements. Coding is considered as the toughest part of the whole recruitment process. Coding is also one of the important topic to cover. It is asked in both Written as well as Technical Interview. Along with Coding few other important topics are mentioned below. Have a look:

Most Asked Coding Questions in Placements

Question: Write code to Reverse of a number

#include
int main()
{
//Initialization of variables where rev='reverse=0'
int number, rev = 0,store, left;

//input a numbers for user
printf("Enter the number\n");
scanf("%d", &number);

store= number;
//use this loop for check true condition
while (number > 0)
{
//left is for remider are left
left= number%10;

//for reverse of no.
rev = rev * 10 + left;

//number /= 10;
number=number/10;

}
//To show the user value
printf("Given number = %d\n",store);

//after reverse show numbers
printf("Its reverse is = %d\n", rev);

return 0;
}
//Reverse of a number
#include
using namespace std;
//main program
int main()
{
//variables initialization
int num, reverse=0, rem;
cout<<"Enter a number: ";
//user input
cin>>num;
//loop to find reverse number
do
{
rem=num%10;
reverse=reverse*10+rem;
num/=10;
}while(num!=0);
//output
cout<<"Reversed Number: "<<reverse;
return 0;
}
import java.util.Scanner;
public class reverse_of_number
{	
	public static void main(String[] args)
	{
		//scanner class declaration
		Scanner sc = new Scanner(System.in);
		//input from user
		System.out.print("Enter a number : ");				
		int number = sc.nextInt();
		System.out.print("Reverse of "+number+" is ");
		int reverse = 0;
		String s = "";
		while(number != 0)
		{
			int pick_last = number % 10;
			//use function to convert pick_last from integer to string
			s = s + Integer.toString(pick_last);
			number = number / 10;	
		}
		//display the reversed number
		System.out.print(s);
		//closing scanner class(not compulsory, but good practice)
		sc.close();													
	}
}
num = int(input("Enter the Number:"))
temp = num
reverse = 0
while num > 0:
    remainder = num % 10
    reverse = (reverse * 10) + remainder
    num = num // 10

print("The Given number is {} and Reverse is {}".format(temp, reverse))

Question: Write code of Factorial of a Number

#include 
int main()
{
//initialize of variable
int
i, number, fact = 1;

//to take user input.
printf("Enter a number to calculate its factorial\n");
scanf("%d", &number);

//use this loop of following statement
for (i = 1; i<= number;i++)
fact = fact * i;

//display of factorial of a given number
printf("Factorial of a number %d is = %d\n", number, fact);

return 0;
}
//Factorial of a number
#include<iostream>
using namespace std;
//main program
int main()
{
//initializing variables
int fact=1,num;
cout<<"Enter the number: ";
//user input
cin>>num;
//checking for negative input
if(num<0)
cout<<"Invalid input!!\nEnter whole numbers only";
// for positive numbers
else
{
for(int i=num;i>0;i--)
{
fact*=i;
}
cout<<"Factorial of "<<num<<" is "<<fact;
}
return 0;
}
import java.util.Scanner;
public class factorial
{
public static void main(String[] args)
{
//scanner class declaration
Scanner sc = new Scanner(System.in);
//input from user
System.out.print("Enter a number : ");
int number = sc.nextInt();
if(number >= 0)
{
//declare a variable to store factorial
int fac = 1;
for(int i = number ; i >= 1 ; i--)
fac = fac * i;
//display the result
System.out.println("Factorial of "+number+" is "+fac);
//closing scanner class(not compulsory, but good practice)
}
else
System.out.println("Value is not defined, please re-enter the value");
sc.close();
}
}
num = int(input("Enter the number:"))
factorial = 1
for i in range(1, num+1):
    factorial = factorial * i

print("Factorial of a Given Number:", factorial)

Question: Write code of Greatest Common Divisor 

// The code used a recursive function to return gcd of p and q 
 int gcd(int p, int q) 
 { 

   // checking divisibility by 0 
    if (p == 0) 
       return q; 
if (q == 0) return p; // base case if (p == q) return p;
// p is greater if (p > q) return gcd(p-q, q); else return gcd(p, q-p); } // Driver program to test above function int main() {
int p = 98, q = 56; printf("GCD of %d and %d is %d ", p, q, gcd(p, q)); return 0; }
//C++ Program
//GCD of Two Numbers
#include
using namespace std;
// Recursive function declaration
int findGCD(int, int);
// main program
int main()
{
int first, second;
        cout<<"Enter First Number: ";
        cin>>first;
        cout<<"Enter second Number: ";
cin>>second;
        cout<<"GCD of "<<first<<" and "<<second<<" is "<<findGCD(first,second);
        return 0;
}
//body of the function
int findGCD(int first, int second)
{
        // 0 is divisible by every number
        if(first == 0)
       {    
           return second;
       }
       if(second == 0)
        {    
           return first;
       }
       // both numbers are equal
        if(first == second)
        {
           return first;
        }
        // first is greater
        else if(first > second)
        {
           return findGCD(first - second, second);
       }
       return findGCD(first, second - first);
}
import java.util.Scanner;
public class gcd_or_hcf
{
public static void main(String[] args)
{
//scanner class declaration
Scanner sc = new Scanner(System.in);
//input from the user
System.out.print("Enter the first number : ");
int num1 = sc.nextInt();
//input from the user
System.out.print("Enter the second number : ");
int num2 = sc.nextInt();
int n = 1;
System.out.print("HCF of "+num1+" and "+num2+" is ");
if( num1 != num2)
{
while(n != 0)
{
//storing remainder
n = num1 % num2;
if(n != 0)
{
num1 = num2;
num2 = n;
}
}
//result
System.out.println(num2);
}
else
System.out.println("Wrong Input");
//closing scanner class(not compulsory, but good practice)
sc.close();
}
}
num1 = int(input("Enter First Number:"))
num2 = int(input("Enter Second Number:"))


def gcdFunction(num1, num2):
    if num1 > num2:
        small = num2
    else:
        small = num1
    for i in range(1, small+1):
        if (num1 % i == 0) and (num2 % i == 0):
            gcd = i
    print("GCD of two Number: {}".format(gcd))

gcdFunction(num1, num2)

Question: Write code of  Perfect number

#include
int main()
{
// Initialization of variables
int number,i=1,total=0;

// To take user input
printf("Enter a number: ");
scanf("%d",&number);

while(i<number)
{
if(number%i==0)
{
total=total+i;
i++;
}
}
//to condition is true
if(total==number)
//display
printf("%d is a perfect number",number);
//to condition is false
else
//display
printf("%d is not a perfect number",number);

return 0;
}
#include
using namespace std;
//main Program
int main ()
{
int div, num, sum=0;
cout << "Enter the number to check : ";
//user input
cin >> num;
//loop to find the sum of divisors
for(int i=1; i < num; i++)
{
div = num % i;
if(div == 0)
sum += i;
}
//checking for perfect number
if (sum == num)
cout<< num <<" is a perfect number.";
else
cout<< num <<" is not a perfect number.";
return 0;
}
import java.util.Scanner;
public class perfect_number_or_not
{
public static void main(String[] args)
{
//scanner class declaration
Scanner sc = new Scanner(System.in);
//input from user
System.out.print("Enter a number : ");
int number = sc.nextInt();
//declare a variable to store sum of factors
int sum = 0;
for(int i = 1 ; i < number ; i++)
{
if(number % i == 0)
sum = sum + i;
}
//comparing whether the sum is equal to the given number or not
if(sum == number)
System.out.println("Perfect Number");
else
System.out.println("Not an Perfect Number");
//closing scanner class(not compulsory, but good practice)
sc.close();
}
}

Question: Write code to Check if two strings are Anagram or not

#include 

int main()
{
    //Initializing variables.
    char str[100];
    int i;
    int freq[256] = {0};
    
    //Accepting inputs.
    printf("Enter the string: ");
    gets(str);
    
    //Calculating frequency of each character.
    for(i = 0; str[i] != '\0'; i++)
    {
        freq[str[i]]++;
    }
    
    
    printf("The non repeating characters are: ");
    for(i = 0; i < 256; i++)
    {
        if(freq[i] == 1)//Finding uniques charcters and printing them.
        {
            printf(" %c ", i);
        }
    }
    return 0;
}
#include
using namespace std;
//main Program
int main ()
{
int div, num, sum=0;
cout << "Enter the number to check : ";
//user input
cin >> num;
//loop to find the sum of divisors
for(int i=1; i < num; i++)
{
div = num % i;
if(div == 0)
sum += i;
}
//checking for perfect number
if (sum == num)
cout<< num <<" is a perfect number.";
else
cout<< num <<" is not a perfect number.";
return 0;
}
import java.util.Scanner;
public class perfect_number_or_not
{
public static void main(String[] args)
{
//scanner class declaration
Scanner sc = new Scanner(System.in);
//input from user
System.out.print("Enter a number : ");
int number = sc.nextInt();
//declare a variable to store sum of factors
int sum = 0;
for(int i = 1 ; i < number ; i++)
{
if(number % i == 0)
sum = sum + i;
}
//comparing whether the sum is equal to the given number or not
if(sum == number)
System.out.println("Perfect Number");
else
System.out.println("Not an Perfect Number");
//closing scanner class(not compulsory, but good practice)
sc.close();
}
}

Question: Write code Check if the given string is Palindrome or not 

#include 
#include 

int main()
{
    //Initializing variable.
    char str[100];  
    int i,length=0,flag=0;

    //Accepting input.
    printf("Enter  the string : ");
    gets(str);
    length=strlen(str);

    //Initializing for loop.
    for(i=0;i<length/2;i++)  
    {
      //Checking if string is palindrome or not.
      if(str[i]==str[length-i-1])
      flag++;

    }
      //Printing result.
      if(flag==i)
 	   printf("String entered is palindrome");
      else
           printf("String entered is not palindrome");

      return 0;
}
#include 
#include 
using namespace std;

int main()
{
    //Initializing variable.
    char str[100];  
    int i,length=0,flag=0;

    //Accepting input.
    cout<<"Enter  the string : "<<endl;
    gets(str);
    length=strlen(str);

    //Initializing for loop.
    for(i=0;i<length/2;i++)  
    {
      //Checking if string is palindrome or not.
      if(str[i]==str[length-i-1])
      flag++;

    }
      //Printing result.
      if(flag==i)
 	cout<<"String entered is palindrome";
      else
        cout<<"String entered is not palindrome";

      return 0;
}
import java.util.Scanner;

public class StringIsAPalindromeOrNot {

	public static void main(String[] args) {
		Scanner sc =new Scanner(System.in);
		 System.out.println("Enter string");
		String s = sc.next();
		String rev = "";
		for (int i = s.length()-1; i >=0 ; i--) 
			rev=rev+s.charAt(i);
		if(s.equals(rev))
			System.out.println("String is palindrome");
		else 
			System.out.println("String is not palindrome");

	}

}
#take user input
String1 = input('Enter the String :')
#initialize string and save reverse of 1st string
String2 = String1[::-1]
#check if both matches
if String1 == String2:
    print('String is palindromic')
else:
    print('Strings is not palindromic')

Questions: Write code  to Calculate frequency of characters in a string 

#include 

int main()
{
    //Initializing variables.
    char str[100];
    int i;
    int freq[256] = {0};
    
    //Accepting inputs.
    printf("Enter the string: ");
    gets(str);
    
    //Calculating frequency of each character.
    for(i = 0; str[i] != '\0'; i++)
    {
        freq[str[i]]++;
    }
    
    //Printing frequency of each character.
    for(i = 0; i < 256; i++)
    {
        if(freq[i] != 0)
        {
            printf("The frequency of %c is %d\n", i, freq[i]);
        }
    }
    return 0;
}
#include 
using namespace std;

int main()
{
    //Initializing variables.
    char str[100];
    int i;
    int freq[256] = {0};
    
    //Accepting inputs.
    cout<<"Enter the string: ";
    gets(str);
    
    //Calculating frequency of each character.
    for(i = 0; str[i] != '\0'; i++)
    {
        freq[str[i]]++;
    }
    
    //Printing frequency of each character.
    for(i = 0; i < 256; i++)
    {
        if(freq[i] != 0)
        {
           cout<<"The frequency of "<<char(i)<<" is "<<freq[i]<<endl;
        }
    }
    return 0;
}
import java.util.Scanner;

public class FrequencyOfCharactersInAString {
   public static void main(String[] args) {
    Scanner sc =new Scanner(System.in);
     System.out.print("Enter String : ");
     String str = sc.nextLine(); 
     int[] freq = new int[str.length()]; 
     int i, j; 

     //Converts given string into character array 
     char string[] = str.toCharArray(); 
     for(i = 0; i <str.length(); i++) { 
        freq[i] = 1; 
          for(j = i+1; j <str.length(); j++) { 
            if(string[i] == string[j]) { 
            freq[i]++; 

           //Set string[j] to 0 to avoid printing visited character 
            string[j] = '0'; 
          } 
       } 
    } 
    //Displays the each character and their corresponding frequency 
    System.out.println("Characters and their corresponding frequencies"); 
    for(i = 0; i <freq.length; i++) { 
       if(string[i] != ' ' && string[i] != '0') 
          System.out.println(string[i] + "-" + freq[i]); 
       } 
   }
}
#take user input
String = input('Enter the string :')
#take character input
Character = input('Enter character :')
#initiaalize int variable to store frequency
frequency = 0
#use count function to count frequency of character
frequency = String.count(Character)
#count function is case sencetive 
#so it print frequency of Character according to given Character
print(str(frequency) + ' is the frequency of given character')

Question: Write code to check if two strings match where one string contains wildcard characters 

#include   
#include  
bool check(char *str1, char * str2) ;// declaration of the check() function
int main() 
{ 
    char str1[100],str2[100];
    printf("Enter first string with wild characters : ");
    gets(str1);
    printf("Enter second string without wild characters : ");
    gets(str2);
    test(str1,str2);
    return 0; 
} 

bool check(char *str1, char * str2) 
{ 
    // checking end of both the strings 
    if (*str1 == '\0' && *str2 == '\0') 
         return true; 
  
    // comparing the characters of both the strings and wild characters(*)
    if (*str1 == '*' && *(str1+1) != '\0' && *str2 == '\0') 
         return false; 
  
    // checking wild characters(?) 
    if (*str1 == '?' || *str1 == *str2) 
         return check(str1+1, str2+1); 
  
    
    if (*str1 == '*') 
         return check(str1+1, str2) || check(str1, str2+1); 
     return false; 
} 
  
// test() function for running test cases
void test(char *str1, char *str2) 
{ 
    check(str1, str2)? puts(" Yes "): puts(" No "); 
    
}
#include 

using namespace std;

int main() 
{
    //Initialize the variables.
    string wild,str;
    
    //Accept the inputs.
    cout<<"Enter string containing wild characters: "; cin>>wild;
    cout<<"Enter string to be matched: "; cin>>str;

    bool TRUE=true,FALSE=false;

    bool check[wild.length()+1][str.length()+1];

    check[0][0]=TRUE;

    for(int i=1;i<=str.length();i++)

       check[0][i]=FALSE;

    for(int i=1;i<=wild.length();i++)

        if(wild[i-1] == '*')//Checking for wild characters.

            check[i][0]=check[i-1][0];

    else

        check[i][0]=FALSE;

    for(int i=1;i<=wild.length();i++)

    {

        for(int j=1;j<=wild.length();j++)

        {

            if(wild[i-1] == str[j-1])

                check[i][j]=check[i-1][j-1];

            else if(wild[i-1] == '?')//Checking for wild character '?'.

                check[i][j]=check[i-1][j-1];

            else if(wild[i-1] == '*')//Checking for wild character '*'.

                check[i][j]=check[i-1][j]||check[i][j-1];
                
            else
               check[i][j] =FALSE;

        }

    }

    //Printing result
    if(check[wild.length()][str.length()])
        cout<<"TRUE";
    else
        cout<<"FALSE</span.";

}
import java.util.Scanner;

public class FrequencyOfCharactersInAString {
   public static void main(String[] args) {
    Scanner sc =new Scanner(System.in);
     System.out.print("Enter String : ");
     String str = sc.nextLine(); 
     int[] freq = new int[str.length()]; 
     int i, j; 

     //Converts given string into character array 
     char string[] = str.toCharArray(); 
     for(i = 0; i <str.length(); i++) { 
        freq[i] = 1; 
          for(j = i+1; j <str.length(); j++) { 
            if(string[i] == string[j]) { 
            freq[i]++; 

           //Set string[j] to 0 to avoid printing visited character 
            string[j] = '0'; 
          } 
       } 
    } 
    //Displays the each character and their corresponding frequency 
    System.out.println("Characters and their corresponding frequencies"); 
    for(i = 0; i <freq.length; i++) { 
       if(string[i] != ' ' && string[i] != '0') 
          System.out.println(string[i] + "-" + freq[i]); 
       } 
   }
}
#take user input
String = input('Enter the string :')
#take character input
Character = input('Enter character :')
#initiaalize int variable to store frequency
frequency = 0
#use count function to count frequency of character
frequency = String.count(Character)
#count function is case sencetive 
#so it print frequency of Character according to given Character
print(str(frequency) + ' is the frequency of given character')

Few

Important Pages to check:

Click on the button provided to visit few more Technical Interview Pages commonly asked during Interview.