Cognizant GenC Next Coding Questions

GenC Next Coding Challenge 2023

Cognizant GenC Next Coding Questions round details are explained in this article. Cognizant has announced its Cognizant GenC Next Hiring for 2023 batch graduates. In this hiring test, coding round is the most important round. It is the second round of the hiring test, and will be conducted through Hackerrank. In this round there will be 5 – 6 questions based on

  • Data Structures
  • Algorithms
  • Competitive Coding
  • SQL
  • JDBC

For  preparing this round we suggest you to practice medium to high difficulty questions of Hackerrank. Here below we have listed some questions based on the pattern of hackerrank and GenC Next, make sure you practice all of them while preparing for GenC Next Coding Round

Cognizant GenC Next Coding Questions 2022

Cognizant GenC Next Coding Questions : Detailed Analysis

Cognizant GenC SectionNo. of questionsTotal Time
MCQ20-2540 mins
Coding Challenge5-6140 mins

Practice Questions for Cognizant GenC Next Coding Challenge

Question 1

Problem statement-: Elliot made a KeyLogger for his friend Romero, so that he can see the passwords of his friend. Keylogger is a software that can tell you the buttons pressed in the keyboard without the consent of the user, and hence unethical. Elliot made it to hack Romero’s passwords. The one problem is, Romero writes the passwords in lowercase characters only, and the keylogger only takes the values of the keys. Like, for a it takes 1, for b 2, and for z 26. For a given number Elliot produces all combinations of passwords in a dictionary and starts a dictionary based password attack. For a given number, print all the possible passwords in a lexicographic order.

Input Format:

  • One line, denoting the value given by the keylogger

Output Format:

  • All possible combinations of keyloggers in new lines are lexicographically ordered.

Constraints:

  • 2<=Number of digit in input<=1000

Sample Input:

1234

Sample Output:

abcd

awd

lcd

Explanation:

For 12, you can take 1,2 that is ab, or you can take l.

Question 2

Problem Statement- Ramesh went to a bookshop to buy books. There is a list of books with their value and price. Now Ramesh has limited money but he wants maximum value possible. Now there are 2 kinds of books, one is denoted with 1, that is independent, another one is denoted as 2, which you have to buy in double, that means you can not buy a single or odd number of those books. 

Print the maximum value Ramesh can extract from the books. 

 

Input Format:

  • First line contains two integers, n (Number of books) and T, total money he has.
  • Then n lines, 4 variables in each line, 
  • The serial number of the book
  • The value of the book
  • The price of the book
  • The marking (1 or 2)

 

Output Format:

  • Maximum value possible to be bought.

 

Constraints:

  • Number of books: <=100
  • Maximum Money with Ramesh <=1000
  • Max price of a book<=1000

 

Sample Input:

5 20

1 3 7 0

3 9 10 1

2 4 3 1

7 3 2 0

22 7 7 0

Sample Output:

20

 

Explanation:

It will be the 1st book,2nd and the third book

Question 3

Problem Statement-  A taxi can take multiple passengers to the railway station at the same time.On the way back to the starting point,the taxi driver may pick up additional passengers for his next trip to the airport.A map of passenger location has been created,represented as a square matrix.

The Matrix is filled with cells,and each cell will have an initial value as follows:

  • A value greater than or equal to zero represents a path.
  • A value equal to 1 represents a passenger.
  • A value equal to -1 represents an obstruction.

The rules of motion of taxi are as follows:

  • The Taxi driver starts at (0,0) and the railway station is at (n-1,n-1).Movement towards the railway station is right or down,through valid path cells.
  • After reaching (n-1,n-1) the taxi driver travels back to (0,0) by travelling left or up through valid path cells.
  • When passing through a path cell containing a passenger,the passenger is picked up.once the rider is picked up the cell becomes an empty path cell. 
  • If there is no valid path between (0,0) and (n-1,n-1),then no passenger can be picked.
  • The goal is to collect as many passengers as possible so that the driver can maximize his earnings.

 

Sample Input 0

4  

0 0 0 1

1 0 0 0

0 0 0 0

0 0 0 0

Sample Output 0

2

Explanation 0

The driver can contain a maximum of 2 passengers by taking the following path (0,0) → (0,1) → (0,2) → (0,3) → (1,3) → (2,3) → (3,3) → (3,2) → (3,1) → (3,0) → (2,0) → (1,0)  → (0,0)

 

 

Sample Input 1

   3   

   3    

   0 1 -1 

   1 0 -1

   1 1 1

Sample Output 1

5

Explanation 1

The driver can contain a maximum of 5 passengers by taking the following path (0,0) → (0,1) → (1,1) → (2,1) → (2,2) → (2,1) → (2,0) → (1,0) → (0,0)

Question 4

Problem Statement – Ratan is a crazy rich person. And he is blessed with luck, so he always made the best profit possible with the shares he bought. That means he bought a share at a low price and sold it at a high price to maximize his profit. Now you are an income tax officer and you need to calculate the profit he made with the given values of stock prices each day. You have to calculate only the maximum profit Ratan earned.

Note that:

  • Ratan never goes into loss.

 

Example 1

  • Price=[1,6,2]
  • Ratan buys it on the first day and sells it on the second. 

Example 2

  • Price=[9,8,6] 

The Price always went down, Ratan never bought it.

 

Input Format:

  • First line with an integer n, denoting the number days with the value of the stack
  • Next n days, telling the price of the stock on that very day.

 

Output Format:

  • Maximum profit done by Ratan in a single line.

 

Constraints:

  • Number of days <=10^8

 

Sample Input for Custom Testing

 

 STDIN              

———–       

7

11

2



Sample Output

  10

 

 Explanation

The maximum profit possible is when Ratan buys it in 1 rupees and sells it in 11.

 

Question 5

Problem Statement – Codu is given a string and he thinks the letters that are repeated do have more power. He gathers only the repeating characters and keeps them as the most powerful to least powerful manner. Now it is your turn to write a code that will help Codu to do that.

Note that: only lowercase alphabets are accepted in input.

 

Input Format:

  • A string in a single line

 

Output Format:

  • A string made of only the repeated characters as sorted their frequency reducin, if the same the lower ascii value comes before.

 

Constraints:

  • Length of string<=10^5

 

Sample Input:

abcdefghaabca

 

Sample Output:

abc

Question 6

Problem Statement – How will we represent a binary tree? We can use a bracket structure for all of the edges, like (Parentnode , Childnode). Now if we use a node in a child node more than once, the tree can not be valid. Same for the parent node, a node can not be taken more than twice in  a graph as a parent node.

Suppose we see this one graph

(P,Q)(P,R)(Q,T)(R,W)(U,V)(Q,S)(R,U)(U,Z)(S,I)(W,Y)

A tree with those edges may be illustrated in many ways.Here are two:

                   P                            P

                /    \                        /     \

              Q      R                   Q      R

            /   \     /    \               /   \    /  \

          S   T   U   W           S    T  U  W

            \        / \     \          /          / \      \

             I     V  Z    Y      I         Z  V    Y

The following is a recursive definition for the S-expression of a tree.

S-exp(node)=(node->val(S-exp(node->first_child))(S-exp(node->second_child))),if node

!NULL=””,node= =NULL

         Where first_child->val<second _child->val(first_child->val is lexicographically than second_child->val)

This tree can be represented in S-expression in multiple ways.The lexicographically smallest way of expressing it as follows:

P(Q(S(I))(T))(R(U(V)(Z))(W(Y))))

 

Translate the node-pair representation into its lexicographically smallest S-expression or report any errors that do not conform to the definition of the binary tree.

The List of errors with their codes is as follows: 

 

Error                               Reason

Code Stopped1                More than 2 children

Code Stopped2                Duplicate Edges

Code Stopped3                Cycle Present(node is direct descendant of more than one node)

Code Stopped4                Multiple Roots

Code Stopped5                Any other error

 

Functional Description

 

Complete the function sExpression in the editor below.

  • The function must return either the lexicographically lowest S-expression or the lexicographically lowest error code as a string.

 

sExpression has the following parameter(s):

  • Nodes:a string of space-separated parenthetical elements,each of which contains the name of two nodes connected by a comma.

 

Constraints:

  1. All node names are single characters in the range ascii[A-Z].
  2. The maximum node count is 26.
  3. There is no specific order to the input (parent,child) pairs.

 

>Input Format for Custom Testing

>Sample Case 0

Sample Input 0

(B,D) (D,E) (A,B) (C,F) (E,G) (A,C)

Sample output 0

(A(B(D(E(G))))(C(F)))

Explanation 0

A representation of tree is as follows:

 

             A

           /    \

         B      C

         /         \

       D           F

      /

     E

    /

  G

>Sample Case 1

Input:

(C,E)(D,F)(A,B)(A,C)(B,K)

Output:

A(B(K))(C(E)))D(F))

Question 7

Problem Statement – Sahil watches TV all day and gets bored. He started playing this dumb game of identifying minimum number of inputs needed to reach a channel. As his cousin, you have to help him, but you live far from his house. So you decide to write a code that will ask Sahil for some inputs and give outputs respectively.

Here are the problems you need to keep in mind,

  • There are 13 buttons on his remote: 10 buttons for the numbers (0-9) to form integers denoting respective channel index, “Up channel” button and  “ Down channel” button for going i +1th channel and i-1th channel from i respectively, and  a “Last viewed” button to see what’s the last channel before it. 
  • The number buttons allow you to jump directly to a specific channel  (Ex: to go to channel 172 by typing 1,7,2).
  • If the channel which you are in is ith and that is the max channel index possible, by Up channel, you will reach the first channel possible. Same goes for the down channel button. You can go to the highest channel possible if you go down from the lowest channel possible.



Sahil can get from one channel to the next in one of the two ways.

Sahil’s parents have set some parental control on some channels on Aniruth’s television. The “Up Channel “ and “Down buttons” buttons skip these channels as these channels are not viewable.

Given a list of channels to view, the lowest channel, the highest channel, and a list of blocked channels, your program should return the minimum number of clicks necessary to get through all the shows that Anirudh would like to match.

 

Input Format

  • First line is the lowest Channel
  • Second-line is the highest Channel
  • Followed by a number of blocked channels B, 
  • and the next B lines contain the actual blocked channels.
  • Followed by the number of Channels to view V, and the next V lines contain the actual channels to view.

 

Constraints 

  • The lowest channel on the television will be greater than 0. and less than or equal to 10,000.
  • The highest channel on the television will be greater than or equal to the lowest channel. and less than or equal to 10.000. 
  • The list of channels that are blocked on Anirudh’s television. All the channels in this list will be valid channels (greater than or equal to lowest channel, less than or equal 1 to highest channel). Duplicates may be Ignored. The blocked list can be a maximum of 40 channels.  
  • The sequence that Sahil must view contains between 1 and 50 elements. inclusive. All channels in this sequence are not in the blocked list and are between lowest channel and highest channel. Inclusive.

 

Sample Input 0:

1

20

2

18

19

5

15

14

17

1

17

Sample output 0:

7

Question 8

Problem Statement –  A company has a list of jobs to perform. Each job has a start time, end time and profit value. The manager has asked his employee Anirudh to pick jobs of his choice. Anirudh being greedy wants to select jobs for him in such a way that would maximize his earnings. 

Given a list of jobs how many jobs and total earning are left for other employees once Anirudh

Picks jobs of his choice.

Note: Anirudh can perform only one job at a time.

 

Input format:

  • Each Job has 3 pieces of info – Start Time,End Time and Profit
  • The first line contains the number of Jobs for the day. Say ‘n’. So there will be ‘3n lines following as each job has 3 lines.
  • Each of the next ‘3n’ lines contains jobs in the following format:
    • start_time
    • end-time
    • Profit

start-time and end-time are in HHMM 24HRS format i.e. 9am is 0900 and 9PM is 2100

 

Constraints

  • The number of jobs in the day i.e’ is less than 10000
  • 0<_n<_10000
  • start-time is always less than end time.

 

Output format :-

  • Program should return an array of 2 integers where
  • 1st one is number of jobs left and earnings of other employees



Sample Input 1 :

4

0200

0300

10

0400

0700

20

0300

0800

30

0900

1000

50

 

Sample Output 1:

1

20

 

Sample Explanation 1

Chooses 1st, 3rd and 4th job cause they don’t overlap. So only second job is remaining.

 

Sample Input 2:

5

0805

0830

100

0835

0900

100

0905

0930

100

0935

1000

100

1005

1030

100

 

Sample output 2:

0

0

 

Sample Explanation 2:

Anirudh can work on all appointments as there are none overlapping.

Hence 0 appointments and 0 earnings for other employees.

Question 9

Problem Statement : Given two integers, the task is to find the hamming distance between two integers. Hamming Distance between two integers is the number of bits that are different at the same position in both numbers.

Example :

Input 1:

a= 10 , b =8

Output 1:

1

Explanation: 10= 1010 , 8=1000 , No of different bits=1.

Input 2:

a=9, b=14

Output 2:

3

Explanation: 9=1001 , 14=1110  , No. of different bits=3.

 

 

 

Question 10

Problem Statement: Given boolean 2D matrix , find the number of islands. A group of connected 1s form an island. For example, the below matrix contains 5 islands.

Example :

m =5, n=5

1  1  0  0  0 

0  1  0  0  1 

1  0  0  1  1 

0  0  0  0  0 

1  0  1  0  1 

Output :

5

Explanation:  There are total 5 island in the matrix.