Cognizant GenC Coding Questions 2025 – Set 1

Cognizant GenC Coding Questions – Set 1 (Beginner to Intermediate)

Previously Asked Cognizant GenC Coding Questions – Set 1 (Beginner to Intermediate)

These coding questions are designed for Cognizant GenC (Basic Track) preparation, reflecting the beginner to intermediate difficulty level seen in recent exams (2022-2024). Solutions are provided in C++ and Python with detailed explanations.

Question 1: Reverse an Array

Given an array of integers, reverse it in-place without using extra space.

Input: arr = [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]

Solution in C++

#include 
using namespace std;

void reverseArray(vector& arr) {
    int left = 0, right = arr.size() - 1;
    while (left < right) {
        swap(arr[left], arr[right]);
        left++;
        right--;
    }
}

int main() {
    vector arr = {1, 2, 3, 4, 5};
    reverseArray(arr);
    for (int x : arr) cout << x << " ";
    cout << endl;
    return 0;
}
            

Solution in Python

def reverse_array(arr):
    left, right = 0, len(arr) - 1
    while left < right:
        arr[left], arr[right] = arr[right], arr[left]
        left += 1
        right -= 1

arr = [1, 2, 3, 4, 5]
reverse_array(arr)
print(arr)
            

Explanation

Approach: Use two pointers (left and right) starting from the ends of the array. Swap elements and move pointers inward until they meet. Time Complexity: O(n/2) = O(n), Space Complexity: O(1).

Relevance: Frequently asked in GenC to test array manipulation and in-place operations.

Question 2: Check Palindrome String

Given a string, check if it is a palindrome (reads the same forward and backward). Ignore case and non-alphanumeric characters.

Input: s = "A man, a plan, a canal: Panama"
Output: true

Solution in C++

#include 
using namespace std;

bool isPalindrome(string s) {
    string clean = "";
    for (char c : s) {
        if (isalnum(c)) clean += tolower(c);
    }
    int left = 0, right = clean.size() - 1;
    while (left < right) {
        if (clean[left] != clean[right]) return false;
        left++;
        right--;
    }
    return true;
}

int main() {
    string s = "A man, a plan, a canal: Panama";
    cout << (isPalindrome(s) ? "true" : "false") << endl;
    return 0;
}
            

Solution in Python

def is_palindrome(s):
    clean = ''.join(c.lower() for c in s if c.isalnum())
    return clean == clean[::-1]

s = "A man, a plan, a canal: Panama"
print(is_palindrome(s))
            

Explanation

Approach: Clean the string by keeping only alphanumeric characters and converting to lowercase. Compare characters from both ends (C++) or reverse the string (Python). Time Complexity: O(n), Space Complexity: O(n) for the cleaned string.

Relevance: Common in GenC to test string handling and two-pointer technique.

Question 3: Find Second Largest Element

Given an array of integers, find the second largest element without sorting the entire array.

Input: arr = [10, 5, 8, 12, 3]
Output: 10

Solution in C++

#include 
using namespace std;

int secondLargest(vector& arr) {
    if (arr.size() < 2) return -1;
    int first = INT_MIN, second = INT_MIN;
    for (int num : arr) {
        if (num > first) {
            second = first;
            first = num;
        } else if (num > second && num != first) {
            second = num;
        }
    }
    return second == INT_MIN ? -1 : second;
}

int main() {
    vector arr = {10, 5, 8, 12, 3};
    cout << secondLargest(arr) << endl;
    return 0;
}
            

Solution in Python

def second_largest(arr):
    if len(arr) < 2:
        return -1
    first = second = float('-inf')
    for num in arr:
        if num > first:
            second = first
            first = num
        elif num > second and num != first:
            second = num
    return second if second != float('-inf') else -1

arr = [10, 5, 8, 12, 3]
print(second_largest(arr))
            

Explanation

Approach: Track the largest (`first`) and second largest (`second`) elements in one pass. Update `second` when a number is less than `first` but greater than current `second`. Time Complexity: O(n), Space Complexity: O(1).

Relevance: Tests array traversal and conditional logic, a staple in GenC coding rounds.

Question 4: Sum of Digits Until Single Digit

Given a number, repeatedly sum its digits until the result is a single digit.

Input: n = 1234
Output: 1 (1+2+3+4 = 10, 1+0 = 1)

Solution in C++

#include 
using namespace std;

int digitalRoot(int n) {
    while (n > 9) {
        int sum = 0;
        while (n > 0) {
            sum += n % 10;
            n /= 10;
        }
        n = sum;
    }
    return n;
}

int main() {
    int n = 1234;
    cout << digitalRoot(n) << endl;
    return 0;
}
            

Solution in Python

def digital_root(n):
    while n > 9:
        n = sum(int(digit) for digit in str(n))
    return n

n = 1234
print(digital_root(n))
            

Explanation

Approach: Extract digits by modulo/division (C++) or string conversion (Python), sum them, and repeat until a single digit remains. Alternatively, use the formula: digital root = 1 + ((n-1) % 9) if n > 0, but iterative is more common in GenC. Time Complexity: O(log n) per digit extraction, Space Complexity: O(1).

Relevance: Tests number manipulation and iteration, recently seen in GenC exams.


Discover more from Jobs Addaa

Subscribe to get the latest posts sent to your email.

Discover more from Jobs Addaa

Subscribe now to keep reading and get access to the full archive.

Continue reading