KEMBAR78
Cognizant Coding Questions | PDF | String (Computer Science) | Theoretical Computer Science
100% found this document useful (3 votes)
5K views9 pages

Cognizant Coding Questions

The document contains two clusters of programming questions, one in Java and the other in Python. The Java questions involve counting special knights based on their power levels and replacing consecutive identical characters in a string. The Python questions focus on identifying triangle substrings and finding characters with specific positional relationships in a string.

Uploaded by

karigeyasri345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
5K views9 pages

Cognizant Coding Questions

The document contains two clusters of programming questions, one in Java and the other in Python. The Java questions involve counting special knights based on their power levels and replacing consecutive identical characters in a string. The Python questions focus on identifying triangle substrings and finding characters with specific positional relationships in a string.

Uploaded by

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

CLUSTER 1 - JAVA


1.Question

In an Arrayland, N knights line up in a row A, each with a unique power level. A knight is
considered special if the number of stronger knights to their left is strictly greater than the
number of stronger knights to their right.

A stronger knight is a knight with a higher power level.

Your task is to write a function that returns the count of special knights in the lineup.

Input Specification

●​ input1: An integer N, representing the number of knights.​

●​ input2: An integer array A of size N, representing the power level of the knights.​

Output Specification

●​ Return an integer representing the number of special knights in the lineup.​

Example 1

Input:

makefile
CopyEdit
input1: 5
input2: [4, 3, 5, 2, 1]

Output:

CopyEdit
2

Explanation:

●​ Knight 1 (4): No one on the left → not special​

●​ Knight 2 (3): 1 stronger on left (4), 1 stronger on right (5) → not special​

●​ Knight 3 (5): No stronger knights → not special​

●​ Knight 4 (2): 3 stronger on left (4, 3, 5), 1 on right (1) → special​

●​ Knight 5 (1): 4 stronger on left (4, 3, 5, 2), 0 on right → special​

Total special knights = 2

Example 2

Input:

makefile
CopyEdit
input1: 4
input2: [1, 2, 3, 4]

Output:

CopyEdit
0

Explanation:​
Every knight has more stronger knights to their right, so none are special.

Code / Answer :

import java.io.*;
import java.util.*;

class UserMainCode {
public int countSpecialKnight(int input1, int[] input2) {
int specialCount = 0;
int n = input1;

// Iterate through each knight


for (int i = 0; i < n; i++) {
int leftStronger = 0;
int rightStronger = 0;

// Count stronger knights to the left


for (int j = 0; j < i; j++) {
if (input2[j] > input2[i]) {
leftStronger++;
}
}

// Count stronger knights to the right


for (int j = i + 1; j < n; j++) {
if (input2[j] > input2[i]) {
rightStronger++;
}
}

// Check if knight is special


if (leftStronger > rightStronger) {
specialCount++;
}
}

return specialCount;
}
}

2.Question

You are given a string S in which you have to replace every group of two or more consecutive
identical characters with a single #. If multiple such groups of characters appear consecutively,
replace them with a single #. Your task is to perform these two operations and return the final
modified string
Input Specification:

Input1:

A string S.

Output Specification:

Return the final replaced string after applying both transformations.

Example 1:

input1 :aabbbccdeeea

Output : #d#a

Explanation:

Here, the string S is "aabbbccdeeea". We can replace the below repeating characters:

aa -> #

bbb -> #

eee -> #

So the final string after replacement is "#ccdee#a".​


Now, we have consecutive '#' characters ("##") which need to be replaced by a single '#'. Thus,
the final output is "#d#a".​

Example 2:

input1 : aaaabbccdd

Output : #bc#

Explanation:

Here, the string S is "aaaabbccdd". We can replace the below repeating characters:

aaaa -> #

bb -> #
dd -> #

So the final string after replacement is "#cc#". Now, we have consecutive '#' characters ("##")
which need to be replaced by a single '#'. Thus, the final output is "#bc#".

Code/ Answer :

CLUSTER 2 - Python

1.Question
You are given a string S of length N. A substring of exactly five characters is considered a
triangle substring if it satisfies the following conditions:
●​ The first character is smaller than the second.

●​ The second character is smaller than the third.

●​ The fourth character is smaller than the third.

●​ The fifth character is smaller than the fourth.

Your task is to find and return an integer value representing the number of triangle substrings
found in the string S.

Input Specification:

input1: A string value S.

Output Specification:

Return an integer value representing the number of triangle substrings found in the string S.

Example 1:

input1: abcbaacdbaca

Output: 2

Explanation:

Here, the string S is "abcbaacdbaca". Below are the valid Triangle Substrings:

●​ abcba -> (Indices: (0,1,2,3,4))


‘a’<’b’
‘b' <’ c’
‘b' < ‘c’
‘a' < ‘b’
●​ acdba -> (Indices: (6,7,8,9,10))

‘a’ < ‘c’


‘c' <’ d ‘
' b' < ‘ d ‘
' a' < ‘b’

Since there are 2 valid substrings, 2 is returned as the output.

Example 2:
input1: hopnm

Output: 1

Explanation:

Here, the string S is "hopnm". Below are the valid Triangle Substrings:

●​ hopnm -> (Indices: (0,1,2,3,4))

'h' < 'o'


'o' < 'p'
'n' < 'p'
'm' < 'n'

Since there is only 1 valid substring 1 is returned as the output.

Code :

class UserMainCode(object):
@classmethod
def countTriangleSubstrings(cls, input1):
count = 0
n = len(input1)

for i in range(n - 4): # loop till length - 5


s = input1[i:i+5]
if s[0] < s[1] < s[2] and s[3] < s[2] and s[4] < s[3]:
count += 1

return count

Question 2

You are given a string S consisting of lowercase English letters and an integer K. Your task is to
find and return a string value representing the first character in the string that has the same
character appearing exactly K positions to its left and K positions to its right.

Note: The value of K is always less than N/2, where N is the length of the string.
Input Specification:

input1: An integer value K, representing the target distance.

input2: A string S consisting of lowercase English letters.

Output Specification:

Return a string value representing the first character in the string that has the same character
appearing exactly K positions to its left and K positions to its right.

Example 1:

input1: 6

input2: he is my intelligent

Output: i

Explanation:

Here, the string S is "he is my intelligent" and K = 6. We can see that

●​ The first occurrence of 'i' is at index 3.


●​ The second occurrence of 'i' is at index 9.
●​ The third occurrence of 'i' is at index 15.

The 'i' at index 9 has another 'i' exactly 6 positions before it (index 3) and 6 positions after it
(index 15). Hence, i is returned as the output.

Example 2:

input1 : 4

input2: she sells sea shells

Output:s

Code:

class UserMainCode(object):
@classmethod
def findChar(cls, input1, input2):
K = input1
S = input2

n = len(S)

for i in range(K, n - K):


if S[i - K] == S[i] == S[i + K]:
return S[i]

return ""



You might also like