- Overview of Cognizant GenC Next Coding Round
- Key Topics to Prepare for Cognizant GenC Next Coding
- Cognizant GenC Next Coding Questions & Solution
- Preparation Strategies for Cognizant GenC Next Coding
- Conclusion
- Frequently Asked Questions (FAQs)
Cognizant GenC Next Coding Questions & Answers 2025 You Must Know
For freshers aspiring to make a mark in the corporate IT sector, Cognizant's GenC Next program is a gateway to a rewarding career. The program's coding round is a pivotal stage that evaluates your technical prowess, logical thinking, and problem-solving capabilities.
This article will walk you through every aspect of the coding test, offering valuable insights, tips, and resources to help you ace the challenge.
Overview of Cognizant GenC Next Coding Round
The coding round is strategically designed to identify candidates with strong analytical and programming skills. Here's what you can expect:
|
Category |
Details |
|
Difficulty Level |
Moderate to Advanced |
|
Duration |
90-120 minutes |
|
Number of Questions |
2-3 questions |
|
Key Topics Covered |
Algorithms, Data Structures, Problem-Solving |
|
Accepted Languages |
Python, Java, C++, JavaScript, etc. |
Key Topics to Prepare for Cognizant GenC Next Coding
Focusing on these topics will enhance your chances of success:
- Data Structures
- Arrays, Linked Lists, Stacks, Queues
- Trees (Binary, BST), Graphs
- Algorithms
- Sorting and Searching Algorithms (QuickSort, MergeSort)
- Dynamic Programming, Greedy Algorithms
- Problem-Solving Techniques
- Optimisation Problems
- Real-life Scenarios
- Programming Concepts
- Object-Oriented Programming (OOP) Principles
- Recursion and Iterative Solutions
- Logical and Analytical Reasoning
- Coding Puzzles
- Pattern Recognition Problems
Cognizant GenC Next Coding Questions & Solution
Problem Statement 1
There are N suns in a galaxy, each sun has M planets, each of the M planets have some number of moons, denoted by galaxy(i)(j), where galaxy(i)(j) denotes the number of moons of the jth planet having the ith sun.
Your task is to determine the maximum number of moons in any solar system. For each sun, calculate the total number of moons across all its planets and output the highest total number of moons found in a single solar system.
Input Format
The first line of input contains two space-separated integers, N and M, representing the number of suns and the number of planets per sun, respectively.
The next N lines of input contains M space separated integers, representing the number of moons for each planet around the respective sun.
Output Format
Display a single integer denoting the maximum total number of moons in a solar system (i.e., across all planets orbiting the same sun).
Solution C++
#include
#include
#include
#include
#include
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n,m;
cin>>n>>m;
int ans=0;
for(int i=0;i<n;i++){
int sum=0;
for(int j=0;j<m;j++){
int a;
cin>>a;
sum+=a;
}
ans=max(ans,sum);
}
cout<<ans<<endl;
return 0;
}
I2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8Y3N0ZGlvPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdWRlIDxhbGdvcml0aG0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgoKaW50IG1haW4oKSB7CiAgICAvKiBFbnRlciB5b3VyIGNvZGUgaGVyZS4gUmVhZCBpbnB1dCBmcm9tIFNURElOLiBQcmludCBvdXRwdXQgdG8gU1RET1VUICovICAgCiAgICBpbnQgbixtOwogICAgY2luPj5uPj5tOwogICAgaW50IGFucz0wOwogICAgZm9yKGludCBpPTA7aTxuO2krKyl7CiAgICAgICAgaW50IHN1bT0wOwogICAgICAgIGZvcihpbnQgaj0wO2o8bTtqKyspewogICAgICAgICAgICBpbnQgYTsKICAgICAgICAgICAgY2luPj5hOwogICAgICAgICAgICBzdW0rPWE7CiAgICAgICAgfQogICAgICAgIGFucz1tYXgoYW5zLHN1bSk7CiAgICB9CiAgICBjb3V0PDxhbnM8PGVuZGw7CiAgICByZXR1cm4gMDsKfQo=
Solution Java
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int a=in.nextInt();
int b=in.nextInt();
int i,j,sum=0,max=0;
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
int k=in.nextInt();
sum=sum+k;
}
if(sum>max)
max=sum;
sum=0;
}
System.out.println(max);
}
}
aW1wb3J0IGphdmEuaW8uKjsKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS50ZXh0Lio7CmltcG9ydCBqYXZhLm1hdGguKjsKaW1wb3J0IGphdmEudXRpbC5yZWdleC4qOwoKcHVibGljIGNsYXNzIFNvbHV0aW9uIHsKCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICBTY2FubmVyIGluPW5ldyBTY2FubmVyKFN5c3RlbS5pbik7CiAgICAgICBpbnQgYT1pbi5uZXh0SW50KCk7CiAgICAgICBpbnQgYj1pbi5uZXh0SW50KCk7CiAgICAgICBpbnQgaSxqLHN1bT0wLG1heD0wOwogICAgICAgZm9yKGk9MDtpPGE7aSsrKQogICAgICAgewogICAgICAgIGZvcihqPTA7ajxiO2orKykKICAgICAgICB7CiAgICAgICAgICAgIGludCBrPWluLm5leHRJbnQoKTsKICAgICAgICAgICAgc3VtPXN1bStrOwogICAgICAgIH0KICAgICAgICBpZihzdW0+bWF4KQogICAgICAgIG1heD1zdW07CiAgICAgICAgc3VtPTA7CiAgICAgICB9CiAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4obWF4KTsKICAgIH0KfQo=
Solution Python
# Enter your code here. Read input from STDIN. Print output to STDOUT
n,m=map(int,input().split())
gal=[]
for i in range(n):
l=list(map(int,input().split()))
gal.append(l)
max=0
s=0
for i in gal:
s=sum(i)
if s>max:
max=s
print(max)
IyBFbnRlciB5b3VyIGNvZGUgaGVyZS4gUmVhZCBpbnB1dCBmcm9tIFNURElOLiBQcmludCBvdXRwdXQgdG8gU1RET1VUDQpuLG09bWFwKGludCxpbnB1dCgpLnNwbGl0KCkpDQpnYWw9W10NCmZvciBpIGluIHJhbmdlKG4pOg0KICAgIGw9bGlzdChtYXAoaW50LGlucHV0KCkuc3BsaXQoKSkpDQogICAgZ2FsLmFwcGVuZChsKQ0KbWF4PTANCnM9MA0KZm9yIGkgaW4gZ2FsOg0KICAgIHM9c3VtKGkpDQogICAgaWYgcz5tYXg6DQogICAgICAgIG1heD1zDQpwcmludChtYXgpDQo=
Problem Statement 2
Melisa likes the XOR operation very much. Recently, her teacher gave her a beautiful array A = [a1, a2, ..., an], where N is the size of the array, and asked her to find the XOR of all XOR values of all possible subarrays. Subarray XORs are the XOR values of all possible subarrays of the array A.
Your task is to compute the XOR of all these subarray XOR values.
Input Format
The first line of input consists of the integer N representing the size of an array.
The second line contains N space-separated integers representing the elements of the array.
Output Format
Print the XOR of all XOR values of all possible subarrays.
Solution C++
#include <bits/stdc++.h>
using namespace std;
// Returns XOR of all subarray Xors
int getTotalXorOfSubarrayXors(vector &arr, int N) {
// Initialize result by 0 as (a XOR 0 = a)
int res = 0;
// Loop over all elements once
for (int i = 0; i < N; i++) {
// Get the frequency of the current element
int freq = (i + 1) * (N - i);
// If frequency is odd, include it in the result
if (freq % 2 == 1) {
res = res ^ arr[i];
}
}
// Return the result
return res;
}
// Driver Code
int main() {
int N;
cin >> N;
vector arr(N);
for (int i = 0; i < N; i++) {
cin >> arr[i];
}
cout << getTotalXorOfSubarrayXors(arr, N);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgovLyBSZXR1cm5zIFhPUiBvZiBhbGwgc3ViYXJyYXkgWG9ycwppbnQgZ2V0VG90YWxYb3JPZlN1YmFycmF5WG9ycyh2ZWN0b3I8aW50PiAmYXJyLCBpbnQgTikgewogICAgLy8gSW5pdGlhbGl6ZSByZXN1bHQgYnkgMCBhcyAoYSBYT1IgMCA9IGEpCiAgICBpbnQgcmVzID0gMDsKCiAgICAvLyBMb29wIG92ZXIgYWxsIGVsZW1lbnRzIG9uY2UKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgTjsgaSsrKSB7CiAgICAgICAgLy8gR2V0IHRoZSBmcmVxdWVuY3kgb2YgdGhlIGN1cnJlbnQgZWxlbWVudAogICAgICAgIGludCBmcmVxID0gKGkgKyAxKSAqIChOIC0gaSk7CgogICAgICAgIC8vIElmIGZyZXF1ZW5jeSBpcyBvZGQsIGluY2x1ZGUgaXQgaW4gdGhlIHJlc3VsdAogICAgICAgIGlmIChmcmVxICUgMiA9PSAxKSB7CiAgICAgICAgICAgIHJlcyA9IHJlcyBeIGFycltpXTsKICAgICAgICB9CiAgICB9CgogICAgLy8gUmV0dXJuIHRoZSByZXN1bHQKICAgIHJldHVybiByZXM7Cn0KCi8vIERyaXZlciBDb2RlCmludCBtYWluKCkgewogICAgaW50IE47CiAgICBjaW4gPj4gTjsKCiAgICB2ZWN0b3I8aW50PiBhcnIoTik7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IE47IGkrKykgewogICAgICAgIGNpbiA+PiBhcnJbaV07CiAgICB9CgogICAgY291dCA8PCBnZXRUb3RhbFhvck9mU3ViYXJyYXlYb3JzKGFyciwgTik7CiAgICByZXR1cm4gMDsKfQo=
Solution Java
import java.util.Scanner;
import java.util.Vector;
public class Main {
// Function to return XOR of all subarray XORs
public static int getTotalXorOfSubarrayXors(Vector arr, int N) {
// Initialize result by 0 as (a XOR 0 = a)
int res = 0;
// Loop over all elements once
for (int i = 0; i < N; i++) {
// Get the frequency of current element
int freq = (i + 1) * (N - i);
// If frequency is odd, include it in the result
if (freq % 2 == 1)
res ^= arr.get(i);
}
// Return the result
return res;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input the size of the array
int N = scanner.nextInt();
// Create a vector to store the elements
Vector arr = new Vector<>(N);
// Input the elements of the array
for (int i = 0; i < N; i++) {
arr.add(scanner.nextInt());
}
// Get the result and print it
System.out.println(getTotalXorOfSubarrayXors(arr, N));
scanner.close();
}
}
aW1wb3J0IGphdmEudXRpbC5TY2FubmVyOwppbXBvcnQgamF2YS51dGlsLlZlY3RvcjsKCnB1YmxpYyBjbGFzcyBNYWluIHsKCiAgICAvLyBGdW5jdGlvbiB0byByZXR1cm4gWE9SIG9mIGFsbCBzdWJhcnJheSBYT1JzCiAgICBwdWJsaWMgc3RhdGljIGludCBnZXRUb3RhbFhvck9mU3ViYXJyYXlYb3JzKFZlY3RvcjxJbnRlZ2VyPiBhcnIsIGludCBOKSB7CiAgICAgICAgLy8gSW5pdGlhbGl6ZSByZXN1bHQgYnkgMCBhcyAoYSBYT1IgMCA9IGEpCiAgICAgICAgaW50IHJlcyA9IDA7CgogICAgICAgIC8vIExvb3Agb3ZlciBhbGwgZWxlbWVudHMgb25jZQogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgTjsgaSsrKSB7CiAgICAgICAgICAgIC8vIEdldCB0aGUgZnJlcXVlbmN5IG9mIGN1cnJlbnQgZWxlbWVudAogICAgICAgICAgICBpbnQgZnJlcSA9IChpICsgMSkgKiAoTiAtIGkpOwoKICAgICAgICAgICAgLy8gSWYgZnJlcXVlbmN5IGlzIG9kZCwgaW5jbHVkZSBpdCBpbiB0aGUgcmVzdWx0CiAgICAgICAgICAgIGlmIChmcmVxICUgMiA9PSAxKQogICAgICAgICAgICAgICAgcmVzIF49IGFyci5nZXQoaSk7CiAgICAgICAgfQoKICAgICAgICAvLyBSZXR1cm4gdGhlIHJlc3VsdAogICAgICAgIHJldHVybiByZXM7CiAgICB9CgogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIFNjYW5uZXIgc2Nhbm5lciA9IG5ldyBTY2FubmVyKFN5c3RlbS5pbik7CgogICAgICAgIC8vIElucHV0IHRoZSBzaXplIG9mIHRoZSBhcnJheQogICAgICAgIGludCBOID0gc2Nhbm5lci5uZXh0SW50KCk7CgogICAgICAgIC8vIENyZWF0ZSBhIHZlY3RvciB0byBzdG9yZSB0aGUgZWxlbWVudHMKICAgICAgICBWZWN0b3I8SW50ZWdlcj4gYXJyID0gbmV3IFZlY3Rvcjw+KE4pOwoKICAgICAgICAvLyBJbnB1dCB0aGUgZWxlbWVudHMgb2YgdGhlIGFycmF5CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBOOyBpKyspIHsKICAgICAgICAgICAgYXJyLmFkZChzY2FubmVyLm5leHRJbnQoKSk7CiAgICAgICAgfQoKICAgICAgICAvLyBHZXQgdGhlIHJlc3VsdCBhbmQgcHJpbnQgaXQKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oZ2V0VG90YWxYb3JPZlN1YmFycmF5WG9ycyhhcnIsIE4pKTsKCiAgICAgICAgc2Nhbm5lci5jbG9zZSgpOwogICAgfQp9Cg==
Solution Python
def get_total_xor_of_subarray_xors(arr, N):
# Initialize result by 0 as (a XOR 0 = a)
res = 0
# Loop over all elements once
for i in range(N):
# Get the frequency of the current element
freq = (i + 1) * (N - i)
# If frequency is odd, include it in the result
if freq % 2 == 1:
res ^= arr[i]
# Return the result
return res
if __name__ == "__main__":
# Input the size of the array
N = int(input())
# Input the elements of the array
arr = list(map(int, input().split()))
# Get the result and print it
print(get_total_xor_of_subarray_xors(arr, N))
ZGVmIGdldF90b3RhbF94b3Jfb2Zfc3ViYXJyYXlfeG9ycyhhcnIsIE4pOg0KICAgICMgSW5pdGlhbGl6ZSByZXN1bHQgYnkgMCBhcyAoYSBYT1IgMCA9IGEpDQogICAgcmVzID0gMA0KDQogICAgIyBMb29wIG92ZXIgYWxsIGVsZW1lbnRzIG9uY2UNCiAgICBmb3IgaSBpbiByYW5nZShOKToNCiAgICAgICAgIyBHZXQgdGhlIGZyZXF1ZW5jeSBvZiB0aGUgY3VycmVudCBlbGVtZW50DQogICAgICAgIGZyZXEgPSAoaSArIDEpICogKE4gLSBpKQ0KDQogICAgICAgICMgSWYgZnJlcXVlbmN5IGlzIG9kZCwgaW5jbHVkZSBpdCBpbiB0aGUgcmVzdWx0DQogICAgICAgIGlmIGZyZXEgJSAyID09IDE6DQogICAgICAgICAgICByZXMgXj0gYXJyW2ldDQoNCiAgICAjIFJldHVybiB0aGUgcmVzdWx0DQogICAgcmV0dXJuIHJlcw0KDQoNCmlmIF9fbmFtZV9fID09ICJfX21haW5fXyI6DQogICAgIyBJbnB1dCB0aGUgc2l6ZSBvZiB0aGUgYXJyYXkNCiAgICBOID0gaW50KGlucHV0KCkpDQoNCiAgICAjIElucHV0IHRoZSBlbGVtZW50cyBvZiB0aGUgYXJyYXkNCiAgICBhcnIgPSBsaXN0KG1hcChpbnQsIGlucHV0KCkuc3BsaXQoKSkpDQoNCiAgICAjIEdldCB0aGUgcmVzdWx0IGFuZCBwcmludCBpdA0KICAgIHByaW50KGdldF90b3RhbF94b3Jfb2Zfc3ViYXJyYXlfeG9ycyhhcnIsIE4pKQ0K
Problem Statement 3
You are an agent, to stop an attack you have to decipher a piece of encrypted code. The encrypted code contains two lines, each containing a sequence of characters. You know that to decipher the code, you have to merge the two lines by adding characters in alternating sequences. Finally, if one sequence is longer than the other, append it to the end.
Help the agent decrypt the code as soon as possible.
Input Format
The first line of the input contains an integer n — the size of the first sequence.
The second line of the input contains the first sequence.
The third line of the input contains an integer m — the size of the second sequence.
The fourth line of the input contains the second sequence.
Output Format
Print the decoded sequence of characters.
Solution C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int n; cin >> n;
string word1; cin >> word1;
int m; cin >> m;
string word2; cin >> word2;
int l = 0, r = 0;
string str = "";
while(l < n && r < m){
str += word1[l++];
str += word2[r++];
}
while(l < n){
str += word1[l++];
}
while(r < m){
str += word2[r++];
}
cout << str << '\n';
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbigpIHsKICBpbnQgbjsgY2luID4+IG47CiAgc3RyaW5nIHdvcmQxOyBjaW4gPj4gd29yZDE7CiAgaW50IG07IGNpbiA+PiBtOwogIHN0cmluZyB3b3JkMjsgY2luID4+IHdvcmQyOwoKICBpbnQgbCA9IDAsIHIgPSAwOwogIHN0cmluZyBzdHIgPSAiIjsKICB3aGlsZShsIDwgbiAmJiByIDwgbSl7CiAgICBzdHIgKz0gd29yZDFbbCsrXTsKICAgIHN0ciArPSB3b3JkMltyKytdOyAgICAgICAgCiAgfQogIHdoaWxlKGwgPCBuKXsKICAgIHN0ciArPSB3b3JkMVtsKytdOwogIH0KICB3aGlsZShyIDwgbSl7CiAgICBzdHIgKz0gd29yZDJbcisrXTsKICB9CgogIGNvdXQgPDwgc3RyIDw8ICdcbic7CiAgCiAgcmV0dXJuIDA7Cn0K
Solution Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read the size and first sequence
int n = scanner.nextInt();
String word1 = scanner.next();
// Read the size and second sequence
int m = scanner.nextInt();
String word2 = scanner.next();
int l = 0, r = 0;
StringBuilder str = new StringBuilder();
// Alternate between the characters of the two sequences
while (l < n && r < m) {
str.append(word1.charAt(l++));
str.append(word2.charAt(r++));
}
// Append remaining characters of word1
while (l < n) {
str.append(word1.charAt(l++));
}
// Append remaining characters of word2
while (r < m) {
str.append(word2.charAt(r++));
}
// Print the result
System.out.println(str.toString());
scanner.close();
}
}
aW1wb3J0IGphdmEudXRpbC5TY2FubmVyOwoKcHVibGljIGNsYXNzIE1haW4gewogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIFNjYW5uZXIgc2Nhbm5lciA9IG5ldyBTY2FubmVyKFN5c3RlbS5pbik7CgogICAgICAgIC8vIFJlYWQgdGhlIHNpemUgYW5kIGZpcnN0IHNlcXVlbmNlCiAgICAgICAgaW50IG4gPSBzY2FubmVyLm5leHRJbnQoKTsKICAgICAgICBTdHJpbmcgd29yZDEgPSBzY2FubmVyLm5leHQoKTsKCiAgICAgICAgLy8gUmVhZCB0aGUgc2l6ZSBhbmQgc2Vjb25kIHNlcXVlbmNlCiAgICAgICAgaW50IG0gPSBzY2FubmVyLm5leHRJbnQoKTsKICAgICAgICBTdHJpbmcgd29yZDIgPSBzY2FubmVyLm5leHQoKTsKCiAgICAgICAgaW50IGwgPSAwLCByID0gMDsKICAgICAgICBTdHJpbmdCdWlsZGVyIHN0ciA9IG5ldyBTdHJpbmdCdWlsZGVyKCk7CgogICAgICAgIC8vIEFsdGVybmF0ZSBiZXR3ZWVuIHRoZSBjaGFyYWN0ZXJzIG9mIHRoZSB0d28gc2VxdWVuY2VzCiAgICAgICAgd2hpbGUgKGwgPCBuICYmIHIgPCBtKSB7CiAgICAgICAgICAgIHN0ci5hcHBlbmQod29yZDEuY2hhckF0KGwrKykpOwogICAgICAgICAgICBzdHIuYXBwZW5kKHdvcmQyLmNoYXJBdChyKyspKTsKICAgICAgICB9CgogICAgICAgIC8vIEFwcGVuZCByZW1haW5pbmcgY2hhcmFjdGVycyBvZiB3b3JkMQogICAgICAgIHdoaWxlIChsIDwgbikgewogICAgICAgICAgICBzdHIuYXBwZW5kKHdvcmQxLmNoYXJBdChsKyspKTsKICAgICAgICB9CgogICAgICAgIC8vIEFwcGVuZCByZW1haW5pbmcgY2hhcmFjdGVycyBvZiB3b3JkMgogICAgICAgIHdoaWxlIChyIDwgbSkgewogICAgICAgICAgICBzdHIuYXBwZW5kKHdvcmQyLmNoYXJBdChyKyspKTsKICAgICAgICB9CgogICAgICAgIC8vIFByaW50IHRoZSByZXN1bHQKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oc3RyLnRvU3RyaW5nKCkpOwoKICAgICAgICBzY2FubmVyLmNsb3NlKCk7CiAgICB9Cn0K
Solution Python
n = int(input())
word1 = input()
m = int(input())
word2 = input()
l, r = 0, 0
result = []
# Alternate between the characters of the two sequences
while l < n and r < m:
result.append(word1[l])
l += 1
result.append(word2[r])
r += 1
# Append remaining characters of word1
while l < n:
result.append(word1[l])
l += 1
# Append remaining characters of word2
while r < m:
result.append(word2[r])
r += 1
# Print the result
print("".join(result))
biA9IGludChpbnB1dCgpKQ0Kd29yZDEgPSBpbnB1dCgpDQptID0gaW50KGlucHV0KCkpDQp3b3JkMiA9IGlucHV0KCkNCg0KbCwgciA9IDAsIDANCnJlc3VsdCA9IFtdDQoNCiMgQWx0ZXJuYXRlIGJldHdlZW4gdGhlIGNoYXJhY3RlcnMgb2YgdGhlIHR3byBzZXF1ZW5jZXMNCndoaWxlIGwgPCBuIGFuZCByIDwgbToNCiAgICByZXN1bHQuYXBwZW5kKHdvcmQxW2xdKQ0KICAgIGwgKz0gMQ0KICAgIHJlc3VsdC5hcHBlbmQod29yZDJbcl0pDQogICAgciArPSAxDQoNCiMgQXBwZW5kIHJlbWFpbmluZyBjaGFyYWN0ZXJzIG9mIHdvcmQxDQp3aGlsZSBsIDwgbjoNCiAgICByZXN1bHQuYXBwZW5kKHdvcmQxW2xdKQ0KICAgIGwgKz0gMQ0KDQojIEFwcGVuZCByZW1haW5pbmcgY2hhcmFjdGVycyBvZiB3b3JkMg0Kd2hpbGUgciA8IG06DQogICAgcmVzdWx0LmFwcGVuZCh3b3JkMltyXSkNCiAgICByICs9IDENCg0KIyBQcmludCB0aGUgcmVzdWx0DQpwcmludCgiIi5qb2luKHJlc3VsdCkpDQo=
Problem Statement 4
Given an array arr, your task is to replace each element in the array with the greatest element among the elements to its right. The last element of the array should be replaced with -1 since there are no elements to its right. The function should return the modified array after these transformations.
Input Format
The first line contains an integer N, representing the size of the array.
The second line contains N space-separated integers representing the elements of the array.
Output Format
A single line containing the elements of the modified array, with each element replaced by the greatest element among the elements to its right and the last element replaced by -1.
Solution C++
#include
#include
using namespace std;
vector replaceElements(vector& arr) {
int n = arr.size();
int max = -1;
// Iterate from the last element to the first
for (int i = n - 1; i >= 0; --i) {
int temp = arr[i];
arr[i] = max;
if (temp > max) {
max = temp;
}
}
return arr;
}
int main() {
int n;
cin >> n;
vector arr(n);
// Read the input array
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
// Replace elements
vector result = replaceElements(arr);
// Print the result
for (int i = 0; i < n; ++i) {
cout << result[i] << " ";
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdmVjdG9yPGludD4gcmVwbGFjZUVsZW1lbnRzKHZlY3RvcjxpbnQ+JiBhcnIpIHsKICAgIGludCBuID0gYXJyLnNpemUoKTsKICAgIGludCBtYXggPSAtMTsKCiAgICAvLyBJdGVyYXRlIGZyb20gdGhlIGxhc3QgZWxlbWVudCB0byB0aGUgZmlyc3QKICAgIGZvciAoaW50IGkgPSBuIC0gMTsgaSA+PSAwOyAtLWkpIHsKICAgICAgICBpbnQgdGVtcCA9IGFycltpXTsKICAgICAgICBhcnJbaV0gPSBtYXg7CiAgICAgICAgaWYgKHRlbXAgPiBtYXgpIHsKICAgICAgICAgICAgbWF4ID0gdGVtcDsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gYXJyOwp9CgppbnQgbWFpbigpIHsKICAgIGludCBuOwogICAgY2luID4+IG47CiAgICB2ZWN0b3I8aW50PiBhcnIobik7CgogICAgLy8gUmVhZCB0aGUgaW5wdXQgYXJyYXkKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgKytpKSB7CiAgICAgICAgY2luID4+IGFycltpXTsKICAgIH0KCiAgICAvLyBSZXBsYWNlIGVsZW1lbnRzCiAgICB2ZWN0b3I8aW50PiByZXN1bHQgPSByZXBsYWNlRWxlbWVudHMoYXJyKTsKCiAgICAvLyBQcmludCB0aGUgcmVzdWx0CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47ICsraSkgewogICAgICAgIGNvdXQgPDwgcmVzdWx0W2ldIDw8ICIgIjsKICAgIH0KCiAgICByZXR1cm4gMDsKfQo=
Solution Java
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Main
{
public static int[] replaceElements(int[] arr) {
int i = arr.length-1;
int max = -1;
while(i >= 0) {
if(arr[i] > max) {
int t = max;
max = arr[i];
arr[i--] = t;
continue;
}
arr[i--] = max;
}
return arr;
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int[] arr = new int[n];
for(int i = 0 ; i < n ; i++){
arr[i] = scn.nextInt();
}
int[] ans = replaceElements(arr);
for(int i = 0 ; i < ans.length ; i++){
System.out.print(ans[i]+ " ");
}
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgTWFpbgp7CiAgICBwdWJsaWMgc3RhdGljIGludFtdIHJlcGxhY2VFbGVtZW50cyhpbnRbXSBhcnIpIHsKICAgICAgICBpbnQgaSA9IGFyci5sZW5ndGgtMTsKICAgICAgICBpbnQgbWF4ID0gLTE7CiAgICAgICAgd2hpbGUoaSA+PSAwKSB7CiAgICAgICAgICAgIGlmKGFycltpXSA+IG1heCkgewogICAgICAgICAgICAgICAgaW50IHQgPSBtYXg7CiAgICAgICAgICAgICAgICBtYXggPSBhcnJbaV07CiAgICAgICAgICAgICAgICBhcnJbaS0tXSA9IHQ7CiAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgfQogICAgICAgICAgICBhcnJbaS0tXSA9IG1heDsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGFycjsKICAgIH0KICAKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluIChTdHJpbmdbXSBhcmdzKSB0aHJvd3MgamF2YS5sYW5nLkV4Y2VwdGlvbgoJewoJCS8vIHlvdXIgY29kZSBnb2VzIGhlcmUKICAgICAgIAoKICAgICAgICBTY2FubmVyIHNjbiAgPSBuZXcgU2Nhbm5lcihTeXN0ZW0uaW4pOwogICAgICAgIGludCBuID0gc2NuLm5leHRJbnQoKTsKICAgICAgICBpbnRbXSBhcnIgPSBuZXcgaW50W25dOwogICAgICAgIGZvcihpbnQgaSA9IDAgOyBpIDwgbiA7IGkrKyl7CiAgICAgICAgICAgIGFycltpXSA9IHNjbi5uZXh0SW50KCk7CiAgICAgICAgfQogICAgICAgIGludFtdIGFucyA9IHJlcGxhY2VFbGVtZW50cyhhcnIpOwogICAgICAgIGZvcihpbnQgaSA9IDAgOyBpIDwgYW5zLmxlbmd0aCA7IGkrKyl7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnQoYW5zW2ldKyAiICIpOwogICAgICAgIH0KCX0KfQo=
Solution Python
n = int(input())
arr = list(map(int, input().split()))
def replace_elements(arr):
max_val = -1
for i in range(len(arr) - 1, -1, -1):
temp = arr[i]
arr[i] = max_val
if temp > max_val:
max_val = temp
return arr
result = replace_elements(arr)
print(" ".join(map(str, result)))
biA9IGludChpbnB1dCgpKQ0KYXJyID0gbGlzdChtYXAoaW50LCBpbnB1dCgpLnNwbGl0KCkpKQ0KDQpkZWYgcmVwbGFjZV9lbGVtZW50cyhhcnIpOg0KICAgIG1heF92YWwgPSAtMQ0KICAgIGZvciBpIGluIHJhbmdlKGxlbihhcnIpIC0gMSwgLTEsIC0xKToNCiAgICAgICAgdGVtcCA9IGFycltpXQ0KICAgICAgICBhcnJbaV0gPSBtYXhfdmFsDQogICAgICAgIGlmIHRlbXAgPiBtYXhfdmFsOg0KICAgICAgICAgICAgbWF4X3ZhbCA9IHRlbXANCiAgICByZXR1cm4gYXJyDQoNCnJlc3VsdCA9IHJlcGxhY2VfZWxlbWVudHMoYXJyKQ0KcHJpbnQoIiAiLmpvaW4obWFwKHN0ciwgcmVzdWx0KSkpDQo=
Problem Statement 5
A toy manufacturing factory produced N toys in a day. The manager has a list of product IDs and another list containing the sizes of the corresponding toys, each of length N. In other words, the i-th integer in the product IDs list represents the product ID of the i-th toy, and the i-th integer in the sizes list represents the size of the i-th toy.
However, the manager accidentally combined these lists by placing the product IDs list in front of the sizes list, resulting in a single list of 2N elements.
You are an employee, and the manager wants you to recover the data and present it as another list of 2N elements such that the product ID of each toy is followed by its size.
Input Format
The first line of the input contains a single integer N, representing the number of toys produced.
The second line contains 2*N space-separated integers representing the merged list given to you by the manager.
Output Format
Print 2*N space-separated integers representing the list wanted by the manager
Solution C++
#include<bits/stdc++.h>
using namespace std;
int main() {
// read inputs
int n;
cin >> n;
int a[2 * n];
for (int i = 0; i < 2 * n; i++)
cin >> a[i];
vector res;
// For the first n elements, find the corresponding element after n indices
// and push both to the result vector
for (int i = 0; i < n; i++) {
res.push_back(a[i]);
res.push_back(a[i + n]);
}
// print the result
for (int i : res)
cout << i << ' ';
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBtYWluKCkgewogICAgLy8gcmVhZCBpbnB1dHMKICAgIGludCBuOwogICAgY2luID4+IG47CiAgICBpbnQgYVsyICogbl07CiAgICBmb3IgKGludCBpID0gMDsgaSA8IDIgKiBuOyBpKyspCiAgICAgICAgY2luID4+IGFbaV07CgogICAgdmVjdG9yPGludD4gcmVzOyAKCiAgICAvLyBGb3IgdGhlIGZpcnN0IG4gZWxlbWVudHMsIGZpbmQgdGhlIGNvcnJlc3BvbmRpbmcgZWxlbWVudCBhZnRlciBuIGluZGljZXMKICAgIC8vIGFuZCBwdXNoIGJvdGggdG8gdGhlIHJlc3VsdCB2ZWN0b3IKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgcmVzLnB1c2hfYmFjayhhW2ldKTsKICAgICAgICByZXMucHVzaF9iYWNrKGFbaSArIG5dKTsKICAgIH0KCiAgICAvLyBwcmludCB0aGUgcmVzdWx0CiAgICBmb3IgKGludCBpIDogcmVzKQogICAgICAgIGNvdXQgPDwgaSA8PCAnICc7CgogICAgcmV0dXJuIDA7IAp9Cg==
Solution Java
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read inputs
int n = scanner.nextInt();
int[] a = new int[2 * n];
for (int i = 0; i < 2 * n; i++) {
a[i] = scanner.nextInt();
}
ArrayList res = new ArrayList<>();
// For the first n elements, find the
// size of the ith element and push them
// at the end of result
for (int i = 0; i < n; i++) {
res.add(a[i]);
res.add(a[i + n]);
}
// Print the result
for (int i : res) {
System.out.print(i + " ");
}
scanner.close();
}
}
aW1wb3J0IGphdmEudXRpbC5TY2FubmVyOwppbXBvcnQgamF2YS51dGlsLkFycmF5TGlzdDsKCnB1YmxpYyBjbGFzcyBNYWluIHsKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBTY2FubmVyIHNjYW5uZXIgPSBuZXcgU2Nhbm5lcihTeXN0ZW0uaW4pOwoKICAgICAgICAvLyBSZWFkIGlucHV0cwogICAgICAgIGludCBuID0gc2Nhbm5lci5uZXh0SW50KCk7CiAgICAgICAgaW50W10gYSA9IG5ldyBpbnRbMiAqIG5dOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgMiAqIG47IGkrKykgewogICAgICAgICAgICBhW2ldID0gc2Nhbm5lci5uZXh0SW50KCk7CiAgICAgICAgfQoKICAgICAgICBBcnJheUxpc3Q8SW50ZWdlcj4gcmVzID0gbmV3IEFycmF5TGlzdDw+KCk7CiAgICAgICAgLy8gRm9yIHRoZSBmaXJzdCBuIGVsZW1lbnRzLCBmaW5kIHRoZQogICAgICAgIC8vIHNpemUgb2YgdGhlIGl0aCBlbGVtZW50IGFuZCBwdXNoIHRoZW0KICAgICAgICAvLyBhdCB0aGUgZW5kIG9mIHJlc3VsdAogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIHJlcy5hZGQoYVtpXSk7CiAgICAgICAgICAgIHJlcy5hZGQoYVtpICsgbl0pOwogICAgICAgIH0KCiAgICAgICAgLy8gUHJpbnQgdGhlIHJlc3VsdAogICAgICAgIGZvciAoaW50IGkgOiByZXMpIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludChpICsgIiAiKTsKICAgICAgICB9CgogICAgICAgIHNjYW5uZXIuY2xvc2UoKTsKICAgIH0KfQo=
Solution Python
n = int(input())
lis = list(map(int, input().split()))
for i in range(n):
print(lis[i], lis[i+n], end=" ")
biA9IGludChpbnB1dCgpKQ0KbGlzID0gbGlzdChtYXAoaW50LCBpbnB1dCgpLnNwbGl0KCkpKQ0KDQpmb3IgaSBpbiByYW5nZShuKToNCiAgICBwcmludChsaXNbaV0sIGxpc1tpK25dLCBlbmQ9IiAiKQ0K
Are you looking for coding assessment questions related to job placement? Click here to access coding practice sessions from moderate to challenging levels.
Preparation Strategies for Cognizant GenC Next Coding
- Understand the Basics: Strengthen your fundamentals in programming and data structures.
- Practice Daily: Solve problems on available online platforms to improve coding skills while effectively managing time.
- Mock Tests: Simulate the coding test environment to improve time management.
- Analyse Past Trends: Review question patterns from previous years for familiarity.
- Learn Debugging: Familiarize yourself with debugging tools to spot errors quickly.
Conclusion
The Cognizant GenC Next coding round is a test of both your programming expertise and logical thinking. With proper preparation, consistent practice, and a strategic approach, you can excel in this round and move closer to landing your dream job at Cognizant.
Use this guide to steer your preparation in the right direction and embrace the challenges of the coding round with confidence. Good luck!
Frequently Asked Questions (FAQs)
1. What programming languages are accepted?
You can use Python, Java, C++, or JavaScript during the test.
2. What is the best way to prepare for dynamic programming questions?
Practice common DP problems like the Knapsack problem and the Fibonacci sequence.
3. Are the coding questions evaluated automatically?
Yes, most coding tests use automated platforms to evaluate your code for correctness and efficiency.
4. Can I attempt all questions in any order?
Generally, you can choose the order, but it’s wise to tackle simpler problems first.
5. How much weight does the coding round carry in the selection process?
The coding round is crucial as it showcases your technical skills and determines your progression to further rounds.
Disclaimer: While we strive for accuracy, we do not guarantee its completeness or reliability. Readers are encouraged to verify all facts and statistics from the official company website or check independently before making decisions.
Suggested reads:
- A Complete Guide to Cognizant GenC Next 2025: Pattern and Syllabus
- Cognizant GenC Exam Pattern and Syllabus: Guide for Fresher 2025
- Wipro WILP Syllabus: Detailed Syllabus Breakdown & Tips for Freshers
- Wipro Work Integrated Learning Program (WILP) 2025: Freshers' Guide
- Wipro Logical Reasoning Questions: Selected MCQs & Tips for Freshers
Instinctively, I fall for nature, music, humor, reading, writing, listening, traveling, observing, learning, unlearning, friendship, exercise, etc., all these from the cradle to the grave- that's ME! It's my irrefutable belief in the uniqueness of all. I'll vehemently defend your right to be your best while I expect the same from you!
Login to continue reading
And access exclusive content, personalized recommendations, and career-boosting opportunities.
Subscribe
to our newsletter
Blogs you need to hog!
How To Write Finance Cover Letter For Morgan Stanley (+Free Sample!)
55+ Data Structure Interview Questions For 2025 (Detailed Answers)
How To Negotiate Salary With HR: Tips And Insider Advice
Comments
Add comment