Most Asked Coding Questions In Placements

Top 50 Most Asked Coding Questions in Placements

Most Asked Coding Questions in Placements –  Coding Questions are important in both written as well as Interview rounds. Questions provided on this page will help you in preparing for the coding rounds. Below you will find the most important codes in languages like C, C++, Java and Python.

Page Highlights

  • About Coding Questions
  • Top 50 Most Asked Coding Question
top programming interview questions pdf

About Coding Questions

Coding Questions are very important, in both online assessments and technical interviews. Depending on the profile interviewers ask candidates conceptual questions about a program or can ask the candidate to write the whole code for any given question.

Top 50 Most Asked Coding Questions in Placements

Top 50 Most Asked Coding Questions in Placements

1. Write a code to reverse a number

To reverse a number, you need to take the digits of the number and rearrange them in the opposite order.

  • Start by converting the number to a string, reverse that string, and then convert it back to an integer. This will give you the reversed version of the original number.
  • Example: If the number is 908701, take digits from last → 1, 0, 7, 8, 0, 9 → and make it 107809.

2. Write the code to find the Fibonacci series upto the nth term.

This problem asks to generate the Fibonacci sequence up to the nth term. In this sequence, each number is the sum of the two preceding ones, starting from 0 and 1.

  • The goal is to calculate and display all Fibonacci numbers from the 0th to the nth term.
  • Example for n = 10:
    0, 1, 1, 2, 3, 5, 8, 13, 21, 34   

(Writing: Start with 0 and 1 → 0+1=1 → 1+1=2 → 1+2=3 → 2+3=5 → and so on.)

3. Write code of Greatest Common Divisor 

This problem asks to find the Greatest Common Divisor (GCD) of two given numbers. The GCD of two numbers is the largest positive integer that divides both numbers without leaving a remainder.

  • The Euclidean algorithm is a popular method for efficiently computing the GCD.
  • Example: Find GCD of 36 and 60:
    The common divisors of 36 and 60 are 1, 2, 3, 4, 6, 9, 12, 18, 36 and 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60.
    The largest common divisor is 12.

4. Write code of  Perfect number

  • A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding the number itself. 
  • Example: Is 28 a perfect number?
    The divisors of 28 are 1, 2, 4, 7, 14.
    Sum of divisors: 1 + 2 + 4 + 7 + 14 = 28, so 28 is a perfect number.

5. Write code to Check if two strings are Anagram or not

Two strings are called anagrams if they contain the same characters in the same frequencies, but possibly in different orders.

  • This code checks whether two given strings are anagrams of each other.
  • Example: Are “listen” and “silent” anagrams?
    Sort both:

“listen” → “eilnst”

“silent” → “eilnst”
Both are the same, so “listen” and “silent” are anagrams.

6. Write code Check if the given string is Palindrome or not

A palindrome is a word, phrase, or sequence that reads the same backward as forward, ignoring spaces, punctuation, and capitalization.

  • This code checks if a given string is a palindrome.
  • Example for a palindrome:
    “madam” — reads the same backward as forward

7. Write code to Calculate frequency of characters in a string

This problem asks to calculate the frequency of each character in a given string.

  • The goal is to determine how many times each character appears in the string.
  • Example for string “hello”:
    • ‘h’ appears 1 time
    • ‘e’ appears 1 time
    • ‘l’ appears 2 times
    • ‘o’ appears 1 time

8. Write code to check if two strings match where one string contains wildcard characters

This problem checks if two strings match where one string contains wildcard characters. The wildcards are:

  • * for any sequence of characters (including an empty sequence).
  • ? for exactly one character.
  • Example:
    • “he?lo” matches “hello”, but not “healo”.
    • “he*o” matches “hello”, “hero”, or “heyo”.

9. Write a code for bubble sort

Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.

  • The process continues until the list is sorted.
  • Example for list [5, 3, 8, 4, 2]:
    After sorting, the list will be [2, 3, 4, 5, 8].

10. How is the merge sort algorithm implemented?

Merge Sort is a divide-and-conquer algorithm that splits the list into smaller sublists, sorts each sublist, and then merges the sorted sublists.

  • The process continues recursively until the entire list is sorted.
  • Example for list [5, 3, 8, 4, 2]:
    After sorting, the list will be [2, 3, 4, 5, 8].

11. Write to code to check whether a given year is leap year or not.

A leap year is a year that is divisible by 4, but not divisible by 100, unless it is also divisible by 400.

  • This code checks whether a given year is a leap year based on this rule.
  • Example:
    • 2020 is a leap year (divisible by 4).
    • 1900 is not a leap year (divisible by 100 but not 400).
    • 2000 is a leap year (divisible by 400).

12. Find non-repeating characters in a string

This problem asks to find the characters in a string that appear only once, i.e., the non-repeating characters.

  • These characters are unique and do not appear multiple times in the string.
  • Example for string “swiss”:
    • Non-repeating characters are ‘w’ and ‘i’, since ‘s’ repeats.

13. Write a code to replace a substring in a string.

This problem asks to replace a substring within a string with another substring.

  • The goal is to find all occurrences of the target substring and replace them with the desired one.
  • Example for string “hello world”:
    • Replacing “world” with “Python” results in “hello Python”.

14. Write a code for Heap sort.

Heap Sort is a comparison-based sorting algorithm that uses a binary heap data structure. It works by building a max-heap (or min-heap) from the input data and then repeatedly extracting the maximum element from the heap and placing it at the end of the array.

  • This process is done until the heap is empty.
  • Example for list [5, 3, 8, 4, 2]:
    After sorting, the list will be [2, 3, 4, 5, 8].

15. Write a code to replace each element in an array by its rank in the array

This problem asks to replace each element in an array by its rank in the array.

  • The rank of an element is its position in the sorted array (with ties assigned the same rank).
  • Example for array [40, 10, 20, 30]:
    After replacing each element by its rank, the array will be [4, 1, 2, 3] (after sorting, the elements are [10, 20, 30, 40], so ranks are [1, 2, 3, 4]).

16. Write a code to find circular rotation of an array by K positions.

This problem asks to find the circular rotation of an array by K positions.

  • In a circular rotation, elements that are moved from the end of the array are appended to the beginning.
  • Example for array [1, 2, 3, 4, 5] and K = 2:
    After rotating the array by 2 positions, the result will be [4, 5, 1, 2, 3].

17. Write a code to find non-repeating elements in an array.

This problem asks to find the elements in an array that appear only once, i.e., the non-repeating elements.

  • These elements are unique and do not appear multiple times in the array.
  • Example for array [4, 5, 4, 3, 6, 3, 7]:
    • Non-repeating elements are [5, 6, 7], since ‘4’ and ‘3’ repeat.

18. Write a code to check for the longest palindrome in an array.

This problem asks to find the longest palindrome in an array of strings. A palindrome is a word, phrase, or sequence that reads the same backward as forward.

  • The goal is to identify the longest string in the array that is a palindrome.
  • Example for array [“racecar”, “level”, “hello”, “madam”, “world”]:
    The longest palindrome is “racecar”.

19. Write a code to find the factorial of a number.

This problem asks to find the factorial of a given number. The factorial of a non-negative integer n is the product of all positive integers less than or equal to n. It is denoted as n!.

  • For example:

5! = 5 × 4 × 3 × 2 × 1 = 120

0! = 1 (by definition).

20. Write the code to for Armstrong number

An Armstrong number (or Narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits.

  • For example:
    • 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.
    • 370 is an Armstrong number because 3^3 + 7^3 + 0^3 = 370.

21. Write a program to find the sum of Natural Numbers using Recursion.

This problem asks to find the sum of the first n natural numbers using recursion. The sum of the first n natural numbers is given by the formula 1 + 2 + 3 + … + n.

  • For example:

Sum of first 5 natural numbers: 1 + 2 + 3 + 4 + 5 = 15.

22. Write a program to add Two Matrices using Multi-dimensional Array.

This problem asks to add two matrices using a multi-dimensional array. Matrix addition is done by adding corresponding elements of two matrices of the same size.

  • The sum matrix will have the same dimensions as the input matrices.
  • Example for matrices:
    • Matrix A:

[1, 2, 3]
[4, 5, 6]

    • Matrix B:

[7, 8, 9]
[10, 11, 12]

    • The sum of Matrix A and Matrix B:

[1+7, 2+8, 3+9]
[4+10, 5+11, 6+12]

    • Result:

[8, 10, 12]
[14, 16, 18]

23. Write a Program to Find the Sum of Natural Numbers using Recursion.

This problem asks to find the sum of the first n natural numbers using recursion.

  • The sum of the first n natural numbers is calculated by recursively adding the numbers from 1 to n.
  • For example:

Sum of the first 5 natural numbers: 1 + 2 + 3 + 4 + 5 = 15.

24. Write code to check a String is palindrome or not?

This problem asks to check whether a given string is a palindrome or not.

  • A string is considered a palindrome if it reads the same forward and backward, ignoring spaces, punctuation, and case.
  • For example:
    • “racecar” is a palindrome because it reads the same forward and backward.
    • “hello” is not a palindrome because it does not read the same backward.

25. Write a program for Binary to Decimal to conversion

This problem asks to convert a binary number (base 2) to its decimal (base 10) equivalent.

  • Each digit in a binary number represents a power of 2, and the decimal value is the sum of these powers.
  • For example:
    • Binary 101 is equal to Decimal 5 because:
      1 * 2^2 + 0 * 2^1 + 1 * 2^0 = 4 + 0 + 1 = 5.

26. Write a program to check whether a character is a vowel or consonant

This problem asks to check whether a given character is a vowel or a consonant. Vowels are the letters a, e, i, o, u (both uppercase and lowercase). Any other alphabetic character is considered a consonant.

27. Write a code to find an Automorphic number

An Automorphic number is a number whose square ends with the same digits as the number itself.

  • For example:
    • 5 is an Automorphic number because 5^2 = 25, and the last digit is 5.
    • 6 is an Automorphic number because 6^2 = 36, and the last digit is 6.
    • 25 is an Automorphic number because 25^2 = 625, and the last two digits are 25.

28. Write a code to find Find the ASCII value of a character

This problem asks to find the ASCII value of a given character. The ASCII (American Standard Code for Information Interchange) value of a character is its integer representation in the ASCII table.

  • For example:
    • The ASCII value of ‘A’ is 65.
    • The ASCII value of ‘a’ is 97.

29. Write a code to Remove all characters from string except alphabets

This problem asks to remove all characters from a string except for the alphabets (both uppercase and lowercase). This can be done by filtering the string and keeping only the alphabetic characters.

30. Write a code to Print the smallest element of the array

This problem asks to find and print the smallest element in an array. You can do this by iterating through the array and comparing each element to find the smallest one.

  • Example :
    • Given an array: [5, 3, 8, 1, 9, 4]
    • The smallest element in the array is 1 because it is the least value compared to the other elements.

31. Write a code to Reverse the element of the array

This problem asks to reverse the elements of an array. Reversing an array means arranging the elements in the opposite order, so the last element becomes the first and so on.

  • Example :
    • Given an array: [1, 2, 3, 4, 5]
    • After reversing the array, it becomes: [5, 4, 3, 2, 1]
    • The elements are arranged in the opposite order, where the last element becomes the first, and so on.

32. Write a code to Sort the element of the array

This problem asks to sort the elements of an array in ascending order. Sorting arranges the elements in order, with the smallest element first and the largest last.

  • Example :
    • Given an array of random numbers:
      [52, 3, 19, 7, 88, 12, 45, 23, 100, 6, 0, 47]
    • After sorting in ascending order, the array becomes:
      [0, 3, 6, 7, 12, 19, 23, 45, 47, 52, 88, 100]
    • This example contains a mix of larger and smaller numbers, and sorting arranges them from the smallest (0) to the largest (100).

33. Write a code to Sort the element of the array without sort method

This problem asks to sort the elements of an array without using the built-in sort() method. The solution requires using the swap method to manually arrange the array in ascending order.

  • One common way to do this is by using a sorting algorithm like Bubble Sort, where adjacent elements are compared and swapped if they are in the wrong order.
  • Example:

Given an array:
[4, 2, 9, 1]

  • Steps (Bubble Sort with Swap Method):
  • First Pass:
    • Compare 4 and 2. Since 4 > 2, swap them: [2, 4, 9, 1]
    • Compare 4 and 9. No swap needed: [2, 4, 9, 1]
    • Compare 9 and 1. Since 9 > 1, swap them: [2, 4, 1, 9]

After the first pass, the largest element 9 is in its correct position.

  • Second Pass:
    • Compare 2 and 4. No swap needed: [2, 4, 1, 9]
    • Compare 4 and 1. Since 4 > 1, swap them: [2, 1, 4, 9]
    • After the second pass, 4 is now in its correct position.
  • Third Pass:
    • Compare 2 and 1. Since 2 > 1, swap them: [1, 2, 4, 9]
    • Now the array is fully sorted.
  • Final Sorted Array:
    [1, 2, 4, 9]

34. Write a code to Replace a Substring in a string

This problem asks to replace a specific substring within a string with another substring. The task is to identify all occurrences of the substring and replace them with the new one.

  • Example :
    • Input String:
      “I love programming in Python!”
    • Substitute:
      Replace “Python” with “Java”.
    • Output:
      “I love programming in Java!”

35. Write a code to Remove space from a string

This problem asks to remove all spaces from a given string. The task is to remove every occurrence of spaces, including leading, trailing, and any spaces in between the words.

  • Example :
    • Input String:
      “This is a test string”
    • Output:
      “Thisisateststring”

36. Write a code to Count Inversion

This problem asks to count the number of inversions in an array. An inversion in an array is a pair of indices (i, j) such that i < j and arr[i] > arr[j]. The goal is to count how many such pairs exist in the given array.

Example :

  • Given the array [1, 20, 6, 4, 5], we count the number of inversions where a larger number appears before a smaller one.
  • The inversions are: (20, 6), (20, 4), (20, 5), (6, 4), and (6, 5), totaling 5 inversions.
  • Each inversion is counted whenever a pair (i, j) with i < j and arr[i] > arr[j] is found. Thus, the result is 5.

37. Write a code to find consecutive largest subsequence

This problem asks to find the longest consecutive subsequence in an unsorted array. A consecutive subsequence is a sequence of numbers that appear in consecutive order but not necessarily contiguous in the original array.

Example :

  • Input Array:
    [100, 4, 200, 1, 3, 2]
  • Step 1:
    Convert the array to a set for fast lookups:
    {100, 4, 200, 1, 3, 2}
  • Step 2:
    Start checking from each element:
    • For 100: Not part of a sequence, so skip.
    • For 4: Start a sequence. Find 3, 2, and 1 in the set.
    • Sequence found: [1, 2, 3, 4].
  • Step 3:
    The longest consecutive subsequence is [1, 2, 3, 4].

38: Write a Program to Find out the Sum of Digits of a Number.

This problem asks to find the sum of the digits of a given number. The goal is to extract each digit of the number and compute their sum.

Example :

  • Input: 12345
  • Extract and Add:
    • 5 + 4 + 3 + 2 + 1
  • Sum: 15
  • Output: The sum of digits is 15.

39: Write a Program to Find out the Power of a Number

This program calculates the power of a number. Given a base a and exponent b, compute𝑎𝑏
(a raised to the power b).

Example :

  • Input: Base = 2, Exponent = 5
  • Calculation: 2^5 = 2×2×2×2×2
  • Result: 32
  • Output: 2 raised to the power 5 is: 32

40: Write a Program to Find out the Sum of Digits of a Number.

This program calculates the sum of digits of a given number. It repeatedly extracts each digit and adds them together.

Example:

  • Input: 789
  • Digits: 7, 8, 9
  • Sum: 7 + 8 + 9 = 24
  • Output: Sum of digits of 789 is: 24

41: Write a Program to Add two Fractions

This program adds two fractions and gives the simplified result.

Example:

  • Input Fractions: 1/2 and 1/3
  • Steps: Cross multiply and add → result is 5/6
  • Simplified Result: 5/6

42: Write a Program to Find the Largest Element in an Array.

This program finds the largest element present in a given array by comparing each element.

  • It checks all elements one by one and keeps track of the maximum value found so far.

Example:

  • Input Array: [10, 45, 2, 67, 23]
  • Compare elements: Find the biggest number.
  • Largest Element: 67
  • Output: Largest element is: 67

43: Write a Program to Find the Roots of a Quadratic Equation

This program finds the roots of a quadratic equation.

  • It checks if the roots are real, equal, or complex based on calculation results.

Example:

  • Input: a = 1, b = -3, c = 2
  • Process: Find roots. Roots are real and different.
  • Roots:2.0 and 1.0
  • Output: Roots are real and different: 2.0, 1.0

44: Write a Program to Find the Prime Factors of a Number.

This program finds all the prime factors of a given number. A prime factor is a factor that is a prime number.

  • The program checks each number and divides the given number by all possible prime factors until it’s reduced to 1.

Example:

  • Input: 56
  • Process:
    • Divide 56 by 2 → 56 / 2 = 28
    • Divide 28 by 2 → 28 / 2 = 14
    • Divide 14 by 2 → 14 / 2 = 7
    • 7 is prime, so stop here.
    • Prime Factors: 2, 2, 2, 7
  • Output: Prime factors of 56 are: [2, 2, 2, 7]

45: Write a Program to Convert Digits to Words.

This program converts a number (digits) into words. For example, the number 123 should be converted into “One Two Three”.

Example:

  • Input: 123
  • Process:
    • 1 → “One”
    • 2 → “Two”
    • 3 → “Three”
  • Output: Digits 123 in words are: One Two Three

46: Write a Program to Find the Factorial of a Number using Recursion.

This program calculates the factorial of a number using recursion. The factorial of a number n is the product of all positive integers less than or equal to n.

Example:

  • Input: 5
  • Process:
    • 5 * factorial(4)
    • 4 * factorial(3)
    • 3 * factorial(2)
    • 2 * factorial(1) (Base case: factorial of 1 is 1)
  • Output: Factorial of 5 is: 120

47: Write a Program to Reverse an Array

This program reverses the elements of an array. It swaps elements from the beginning and end until the entire array is reversed.

Example:

  • Input: [1, 2, 3, 4, 5]
  • Process:
    • Swap 1 and 5 → [5, 2, 3, 4, 1]
    • Swap 2 and 4 → [5, 4, 3, 2, 1]
    • Array is reversed.
  • Output: Reversed array: [5, 4, 3, 2, 1]

48. Write code to check if two strings match where one string contains wildcard characters

This program checks if two strings match, where one of the strings may contain wildcard characters.

  • The wildcard character * represents any sequence of characters (including an empty sequence), and ? represents any single character.

Example:

  • Input:
    • Pattern: “a*b?e”
    • String: “ababe”
  • Process:
    • * matches “ab”
    • ? matches “b”
  • Output: Do the strings match? True

49: Write a Program to find out the Spiral Traversal of a Matrix.

This program finds the spiral traversal of a matrix, where the elements are visited in a spiral order starting from the top left corner.

  • The traversal follows the pattern: left to right, top to bottom, right to left, and bottom to top, continuously spiraling inward.

Example:

  • Input:
    • Matrix:

[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]

  • Process:
    • First row: 1, 2, 3
    • Last column: 6, 9
    • Last row (reverse): 8, 7
    • First column (reverse): 4
    • Middle element: 5
  • Spiral Traversal: [1, 2, 3, 6, 9, 8, 7, 4, 5]
  • Output:
    • Spiral traversal: [1, 2, 3, 6, 9, 8, 7, 4, 5]

50. Write a code to find Fibonacci Series using Recursion

This program calculates the Fibonacci series using recursion. In the Fibonacci series, each number is the sum of the two preceding ones, starting from 0 and 1.

Example:

  • Input: 10
  • Process:
    • The Fibonacci sequence starts from 0 and 1.
    • For n=10, the series is generated as follows:
      • fibonacci(0) = 0
      • fibonacci(1) = 1
      • fibonacci(2) = 1 (0 + 1)
      • fibonacci(3) = 2 (1 + 1)
      • Continue this until n=10.
  • Fibonacci Series: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
  • Output: Fibonacci series up to 10 terms: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Introduction to Programming

There are primarily four programming languages on which interviewers can ask questions. You can prepare for coding languages here:-

Other than coding languages, the placement process also requires one to learn about DSA.

Most Asked Coding Questions

Below we have given the most commonly asked coding questions in placement and interviews.

  1. Most asked Coding Questions (PrepInsta Top 100 Codes)
  2. Most asked Coding Questions DSA (PrepInsta Top 100 DSA)

Also Check