0% found this document useful (0 votes)
31 views

Cognizant Itt Questions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Cognizant Itt Questions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

COGNIZANT INTERVIEW QUESTIONS

1. Find all the prime numbers within the given range.

2. Given an integer array nums , print an array answer such that answer[i] is
equal to product of all the elements of nums except nums[i].

Input:

4
1234

Output:

24 12 8 6

3. Given two integers, 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.

Input:

3 14

Output:

4. Given an integer array nums and an integer K , return the Kth largest
element in the array that is the Kth largest element in the sorted array not
the Kth distinct element.

Input:

321564
2

Output:

Here, K = 2. The second largest number is 5.

5. Given two strings S1 and S2 , find if first string is a subsequence of second.

Input:

AXY
ADXCPY

Output:
True

6. Given an array ‘ARR’ containing ‘N’ integers’. The task is to find the sum
of the minimum value in ‘SUB’, where ‘SUB’ ranges over every contiguous
subarray of ‘ARR’.

Input:

4
1234

Output:

20

Test case 1:
Subarrays with ‘1’ as their minimum value: [1], [1,2], [1,2,3], [1,2,3,4]. Sum
= 1 + 1 + 1 + 1 = 4.
Subarrays with ‘2’ as their minimum value: [2], [2,3], [2,3,4]. Sum = 2 + 2 +
2 = 6.
Subarrays with ‘3’ as their minimum value: [3], [3,4]. Sum = 3 + 3 = 6.
Subarrays with ‘4’ as their minimum value: [4]. Sum = 4.
The sum over all subarrays is ‘4 + 6 + 6 + 4 = 20’.
Thus, you should return ‘20’ as the answer.

7. Given an input integer ‘N’, print the following binary pattern for it.

Input:

Output:

1111
000
11
0

8. Find all the factors of the given number. . If the entered input is zero then the
output should be “No Factors”. And if the entered input is a negative number
then first convert it to positive and then find its factors.

9. Given a time in 12-hour AM/PM format, convert it to military (24-hour)


time. Don’t use inbuilt functions.

10.Given an integer array, Remove duplicate elements and print the result.

Input:

122445677

Output:
124567

11. Find the sum of numbers without using arithmetic operators.

12.Find the LCM of three numbers.

COMPANY: INFY DIGITAL

1.

You have been given a string S of length N. The given string is a binary string which
consists of only 0’s and ‘1’s. Ugliness of a string is defined as the decimal number that
this binary string represents.

Example:

 “101” represents 5.
 “0000” represents 0.
 “01010” represents 10.

There are two types of operations that can be performed on the given string.

 Swap any two characters by paying a cost of A coins.


 Flip any character by paying a cost of B coins
 flipping a character means converting a ‘1’to a ‘0’or converting a ‘0’ to a ‘1’.

Initially, you have been given coins equal to the value defined in CASH. Your task is to
minimize the ugliness of the string by performing the above mentioned operations on it.
Since the answer can be very large, return the answer modulo 10^9+7.

Note:

 You can perform an operation only if you have enough number of coins to
perform it.
 After every operation the number of coins get deducted by the cost for that
operation.

Input Format
 The first line contains an integer, N, denoting the number of character in the
string
 The next line contains a string, S, denoting the the binary string
 The next line contains an integer, CASH, denoting the total number of coins
present initially
 Next will contains an integer, A, denoting the cost to swap two characters.
 Then the next line contains an integer, B, denoting the cost to flip a character.

Constraints

 1 <= N <= 10^5


 1< len(S)<= 10^5
 1<=CASH <=10^5
 1<=A<=10^5
 1<=B<=10^5

Sample Input 1 :

4
1111
7
1
2

Sample Output 1 :

Explanation:

3 flips can be used to create “0001” which represents 1.

Sample Input 2:

6
111011
7
1
3

Sample Output 2:

Explanation:
First swap 0 with the most significant 1, then use flip twice first on index one and then
on index two “111011”=>”0111111″=>”001111″=>”000111″ the value represented is
7.

Sample Input 3:

6
111011
7
3
2

Sample Output 3:

Explanation:

Flip the 3 most significant characters to get “000011” : the value represented by this
string is 3.N

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

2.

Problem Statement :

Khaled has an array A of N elements. It is guaranteed that N is even. He wants to


choose at most N/2 elements from array A. It is not necessary to choose
consecutive elements. Khaled is interested in XOR of all the elements he chooses.
Here, XOR denotes the bitwise XOR operation.

For example:

 If A=[2,4,6,8], then khaled can choose the subset [2,4,8] to achieve XOR=(2 XOR
4 XOR 8)=14.

Khaled wants to maximize the XOR of all the elements he chooses. Your task is to
help khaled to find the max XOR of a subset that he can achieve by choosing at most
N/2 elements?

Input format:

 The first line contains an integer, N, denoting the number of elements in A.


 Each line i of the N subsequent lines(where 0<=i<=N) contains an integer
describing Ai.
Constraints

 1<=N<=120
 1<=A[i]<=10^6

Sample Input 1

2
1
2
Sample Output 1
2

Explanation:

N=2, A=[1,2] khaled can choose the subset[2]. The xor of the elements in the subset
is 2. And the number of elements in the subset is 1 which is less than N/2.

Sample Input 2

4
1
2
4
7

Sample Output 2

Explanation:

N=4, A=[1,2,4,7] Khaled can choose the subset [7]. The xor of the elements in the
subset is 7, and the number of elements in the subset is 1 which is less than N/2.

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

You might also like