First Naukri Coding Questions With Solutions

First Naukri Coding Questions for Practice 2024

First Naukri Practice Coding questions are present on this page. A lot of companies are conducting there placement drives through First Naukri. Coding Section of First Naukri is one of the important section of their placement drive, if you are appearing for any drive that is being conducted through First Naukri, make sure you are practicing these First Naukri Coding Questions.

First Naukri Coding Questions

First Naukri Coding Questions With Solutions

Question 1 – Reverse The Bits

Problem Statement :-Suresh knows binary numbers very well. Unfortunately ,he forgot his bank account password , but he remembered a number and if we reverse the binary digits of that number we will get his password.Help suresh to find his password. You will be given a variable name n which is a no of passwords to find followed by n space separated numbers.You are required to give as output and passwords showing for each number.

For Example , if n is 3 and numbers 35000,3467,94856 , you are expected to give as output 487653376, 3517972480 and 290357248.

Example

  • Case 1:

       For the input provided as follows :
       3
       35000
       3467
       94856

       Output of the program will be :
       487653376, 3517972480 , 290357248

       Explanation :

        when we reverse the binary digits of number 35000,3467,94856. we get 487653376,3517972480 , 290357248 as the result.

  • Case 2:

        For the input provided as follows :
        2
        32678
        10

        Output of the program will be :
        65536,1342177280

        Explanation :

        when we reverse the binary digits of 32678 and 10. we get 65536 and 1342177280 as the result.

Question 2: Probability of getting heads

Problem Statement :-In a cricket match , the captain of team A is tossing the coin. If head comes, team A wins the toss and similarly if tail comes then team B will win the toss. They will do N tosses of a coin for N different matches. Your task is to complete a function “count_wins()” that takes two inputs N and R. The function should return the probability of getting exactly R head on N successive tosses of a fair coin i.e. you have to find the probability that team A won the toss. coin has a equal probability of landing a head or tail (i.e. 0.5) on each toss.

Example

  • Case 1:
    For the input provided as follows :
    4 3

         The output of the program will be:
         0.250000

  • Case 2:
    For the input provided as follows :
    1 1

          The output of the program will be : 
          0.500000

Question 3 – Ayushman And Books

Problem statement :- Ayushman has plenty of coins with value m. And he will get any specific amount for a single book from his parents. For a given number of books and their price, and the value of the coin, if Ayushman can choose any amount at first, you have to predict one of how many books can be bought by him, and also the values of those books. Given that, Ayushman will maximise the number of books he can buy possibly with the amount he got and the infinity number of coins he has.
Note that, Whichever book he buys, he must use the money he gets from his parents. And the price of the books will be given in a sorted order. And there will only be one set of books that can be maximised as per the needs.

Input Format:
First line with space separated n and m, number of books and the plenty coins’ value if each
Next n lines represent the books with their cost.

Output Format:
First line with k, the number of books out of which, he can buy one.
Next k lines, the cost of them.

Sample Test Case:
6 6
3
4
9
10
13
16

Sample Output:
3
4
10
16

Explanation:

If he chooses 3, then he can buy either 3 or 9 (3+6)
If he chooses 4, he can buy either 4, or 10 or 16.

Question 4: Matrix Path

Problem Statement :- You are in a ground maze and you will have to reach the destination (n-1,n-1). In the maze , you start at the top left (0,0), and make your way down to (n-1,n-1). At each step , you can only allowed to move downwards or rightwards , but if you are on the left diagonal (row number and column number are same) , then you can move down and right in one move.However , you cannot move to the block if it is an obstacle. You can only move if there is a path. 1 denotes an obstacle and 0 denotes the path .You have to print the minimum number of steps to reach the destination from the starting position.

First line of input contains an integer n which represents the number of rows and columns of matrix (it is always a square matrix).This is followed by n space separated number in n lines.

Example 

  • Case 1:

         For the input provided as follows :
         4
        0 1 1 1
        0 0 0 1
        0 1 0 0
        0 0 0 0

       The output of the program will be
       3

       Explanation :

       Minimum number of steps required is 3 steps:

  1. Step 1: (0,0)->(1,1)
  2. Step 2: (1,1)->(2,2)
  3. Step 3: (2,2) ->(3,3)
  • Case 2:

          For the input provided as follows:
          4
          0 0 0 1
          0 0 1 0
          0 0 1 0
          1 0 0 0

         The output of the program will be:
          5

          Explanation :
          Minimum number of steps required is 5 steps:
          The steps are : (0,0)->(1,1); (1,1)->(2,1);(2,1)->(3,1);(3,1)->(3,2);(3,2)->(3,3)

Question 5: Is Sum Possible

Problem statement :- In a shop there are n items with different prices for each item.You have total N Rs and you will have to purchase a pair of items with that amount. The function should return true, if there exists a pair of items in the shop and whose sum adds to N Rs, else return false. The items are NOT necessarily in sorted order. Your task is to write the body of isSumPossible function.

The input arguments for the function are:
Number of items in the shop (x)
The total amount of money to be compared (N)
Items prices separated by space

  • For example
    For the input provided to the program as follows:
    10
    77
    11 20 33 40 55 60 66 70 75 80

         The output of the program will be :
          true

          Explanation :
          11+66=77 . There exists a pair of items whose sum is N.

  • Another example
    For the input provided to the program as follows:
    9
    27
    4 7 15 8 6 22 1 3 11

         The output of the program will be:
          false

         Explanation :
          No possible combination of 2 items would lead to a sum 27.

Question 6: Product Of Diagonals

Problem Statement :- Write a program that will print the product of elements of left diagonal or right diagonal whose product is Maximum.The program takes total n rows and n columns as input (n numbers will be inputed per line and each number will be separated by space).

  • For example
    For the input provided as follows:
    3
    1 2 3
    4 5 6
    7 8 9

         The program will provide the following output:
          105

         Explanation :

         The product of left diagonal is 45 and the right diagonal is 105. So the output is the greatest of these two which is 105. 

  • Another example,
    5
    1 2 3 4 5
    5 1 2 3 4
    4 5 1 2 3
    3 4 5 1 2
    2 3 4 5 1

         The program will provide the following output :
         120

         Explanation :

         The product of left diagonal is 1 and the product of right diagonal is 120. so the greatest of these two is 120.

 

Question 7 – Vaibhav’s Password

Problem statement :- Vaibhav is setting a password for his facebook account. He want that password to be a absolute unique one, so that it becomes nearly impossible for the hackers or his friends or foe’s to crack it. He came up with a idea, that first he will write a string of 14 characters of his choice, and then after that he will change the adjacent occurrences of characters of english alphabet with #.Like in a string “abskl” , ‘a’ and ‘b’ are adjacent to each other so we have to change them to # symbol i.e. our string becomes #skl. Help him to write a code for doing the same

For example ,

  • Case 1:
    For the input provided as follows:
    acdgelskelvnuv

         Output of the program will be :
         a#gelskelvn#

  • Case 2:
    For the input provided as follows:
    jkasdoghqwerty

         Output of the program will be :
         #asdo#qwerty

Question 8: Balanced Parentheses 

The Mathematics teacher gave his students a task to check if the arithmetic equation has balanced parentheses in it or not. An equation is said to be balanced if all the left parenthesis ‘(’ has a matching right parenthesis ‘)’ and the count of both should be equal.If so , the program will print 0 else the program will print 1.

  • For example ,
    For the input provided as follows:
    ((a+b)*c-d/e)

         The output of the program will be :
          0

          Explanation :

          All the parentheses of this equation are balanced . Hence the output is 0.

  • Another example ,
    For the input provided as follows :
    (9*(7-2)*(1*5)

         The output of the program will be :
          1

         Explanation :

         All the parentheses of this equation are not balanced . Hence the output is 1.

Question 9 – Sum Of Non-Prime

Problem Statement :- Write a program to display the sum of all non-prime numbers from the given list of integers. A prime number is a number which is divisible by 1 or itself.
First line contains a number indicating the total number of integers and the second line contains integers separated by spaces.

  • For example ,
    For the input provided as follows:

          5
          4 6 9 3 7

         The output of the program will be:
         19

         Explanation :

        Non-prime number in the list are 4,6,9. So by adding these numbers we get  4+6+9=19.

  • Another Example ,

        For the input provided as follows
        10
        4 30 7 23 5 99 3 45 19 17

        The output of the program will be :
        178

         Explanation :

         Non-prime number in the list are 4,30,99,45 and the sum of all these number is 178.

Question 10 –  Percentage in Fraction

Problem Statement :- Ankit got his 12th board examination result , but in result only subject names and marks are mentioned, there are 5 subjects in total with total 100 marks each. You have to calculate his percentage in fraction like if the percentage are 72.5% then you have to write it as 72 ½ .Ankit is asking for your help.Write a program to calculate his percentage in fraction.

First five lines of input contain marks of five subjects and you have to calculate the percentage in fraction.

  • For example ,
    For the input provided as follows:
    70 77 73 72 80

          The output of the program will be:
           74 2/5

          Explanation:
          The percentage of above five subject is (70+77+73+72+80)/5 = 74.4 which in fraction is represented as 74 2/5

  • Another Example ,

           For the input provided as follows
           33 45 67 89 38

           The output of the program will be :
           54 2/5
           Explanation :
           The percentage of above five subject marks is (33+45+67+89+38)/5=54.4 which in fraction is represented as 54 2/5

Question 11 –  Search For Cube Root’s

Problem Statement :- Write a program to find the sum of all the numbers whose cube roots are available in the list.If the number is not available then display “Not Found” as an output.

 

  • For example ,
    1,8,7,125,5,211,343,25,400,512

         The output of the program will be:
           981

         Explanation :
         The numbers whose cube roots is present in the list are 1+125+343+512=981

  • Another example ,
    4,9,343,25,78,512

         The output of the program will be:
          Not Found

         Explanation:
         No cube root is present in the list . Hence the answer is “Not Found”.

Question 12 –  Sorted String 

Problem Statement :- There are n number of words in the string separated by space.Your task is to arrange all these words according to their ASCII value, if let’s say there are three words in string i.e “I am good” then then the output of the program according to their ASCII value is “am good I”.It is given that all the letters of the string is in lowercase.

First line of input contains n which is the number of words in the string followed by a string.

  • For example ,
    5
    abc hki gho hkc xyz

         The output of the program will be
          abc gho hkc hki xyz

  • Another example
    i am coding in java

          The output of the program will be
           am coding i in java

Question 13 –  Occurrences Count

Problem Statement :- Write a program to read a sentence and output the number occurrences of each word in a sentence.The output of the program should be sorted into ascending order. It is given that all the letters of the words are in lowercase only and separated by spaces (“ ”). Display all the words in ascending order with their count.

Example

  • Case 1:
    For the input given below
    you are good when your thoughts are good and your deeds are good

          The output of the program will be
          and – 1
          are – 3
          deeds – 1
          good – 3
          thoughts – 1
          when – 1
          you – 1
          your – 2

  • Case 2:
    For the input given below
    if you fail to plan you are planning to fail.

         The output of the program will be
          are – 1
          fail – 2
          if – 1
          plan -1
          planning – 1
          to – 2
         you – 2

Question 14 –  Count Set Bits

Problem Statement :- Write a program to count the number of sets bits in the binary representation of an integer. First line of input contains an integer n .The output of the program should be the number of set bits of a binary representation of a number.

Examples:

  • Case 1:
    For the input provided as follows
    7

         The output of the program will be
          3

        Explanation
        Binary representation of 7 is 111 and has 3 set bits.

  • Case 2:
    For the input provided as follows
    17

          The output of the program will be
          2

         Explanation
         Binary representation of 17 is 10001 and has 2 set bits.

Question 15 – Maximum Difference 

Given below the heights of students according to their standing order in a queue, find the maximum difference between heights of two students such that the smaller student stands before the larger student.The first line of input contain and integer n which is the number of elements in the array followed by n space separated elements of array.The output of the program should be the maximum difference of two array elements that satisfies the above condition.

  • Example ,
    For the input given below
    7
    3 8 10 6 2 4 6

        The output of the program will be
        7

       Explanation:
       The maximum difference between 3 and 10 is 7.

  • Another Example,
    9
    3 5 2 9 5 8 6 12 7

        The output of the program will be
        10

        Explanation:
        The maximum difference between 2 and 12 is 10.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others