Cognizant GenC Coding Questions 2025 – Set 3

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

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

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

Question 1: Find Missing Number in Array

Given an array of n-1 integers in the range 1 to n with no duplicates, find the missing number.

Input: arr = [1, 2, 4, 6, 3, 7, 8], n = 8
Output: 5

Solution in C++

#include 
using namespace std;

int findMissingNumber(vector& arr, int n) {
    int expectedSum = (n * (n + 1)) / 2;
    int actualSum = 0;
    for (int num : arr) actualSum += num;
    return expectedSum - actualSum;
}

int main() {
    vector arr = {1, 2, 4, 6, 3, 7, 8};
    int n = 8;
    cout << findMissingNumber(arr, n) << endl;
    return 0;
}
            

Solution in Python

def find_missing_number(arr, n):
    expected_sum = (n * (n + 1)) // 2
    actual_sum = sum(arr)
    return expected_sum - actual_sum

arr = [1, 2, 4, 6, 3, 7, 8]
n = 8
print(find_missing_number(arr, n))
            

Explanation

Approach: Calculate the expected sum of numbers from 1 to n using the formula n*(n+1)/2. Subtract the actual sum of the array to find the missing number. Time Complexity: O(n), Space Complexity: O(1).

Relevance: Mathematical array problems are common in GenC to test logical reasoning.

Question 2: Swap Characters to Make Strings Equal

Given two strings of equal length, check if they can be made equal by swapping two characters in one string.

Input: s1 = "abcd", s2 = "adcb"
Output: true (Swap 'b' and 'd' in s1)

Solution in C++

#include 
using namespace std;

bool canBeEqual(string s1, string s2) {
    if (s1.size() != s2.size()) return false;
    vector diff;
    for (int i = 0; i < s1.size(); i++) {
        if (s1[i] != s2[i]) diff.push_back(i);
        if (diff.size() > 2) return false;
    }
    if (diff.size() == 0) return true;
    if (diff.size() != 2) return false;
    return s1[diff[0]] == s2[diff[1]] && s1[diff[1]] == s2[diff[0]];
}

int main() {
    string s1 = "abcd", s2 = "adcb";
    cout << (canBeEqual(s1, s2) ? "true" : "false") << endl;
    return 0;
}
            

Solution in Python

def can_be_equal(s1, s2):
    if len(s1) != len(s2):
        return False
    diff = []
    for i in range(len(s1)):
        if s1[i] != s2[i]:
            diff.append(i)
        if len(diff) > 2:
            return False
    if len(diff) == 0:
        return True
    if len(diff) != 2:
        return False
    return s1[diff[0]] == s2[diff[1]] and s1[diff[1]] == s2[diff[0]]

s1, s2 = "abcd", "adcb"
print(can_be_equal(s1, s2))
            

Explanation

Approach: Compare strings and collect positions of differences. If exactly two positions differ and swapping them aligns the strings, return true. Time Complexity: O(n), Space Complexity: O(1) or O(n) for diff array.

Relevance: String comparison with constraints is a typical GenC problem.

Question 3: Check Prime Number

Given an integer, check if it is a prime number (divisible only by 1 and itself).

Input: n = 17
Output: true

Solution in C++

#include 
using namespace std;

bool isPrime(int n) {
    if (n <= 1) return false;
    if (n == 2) return true;
    if (n % 2 == 0) return false;
    for (int i = 3; i <= sqrt(n); i += 2) {
        if (n % i == 0) return false;
    }
    return true;
}

int main() {
    int n = 17;
    cout << (isPrime(n) ? "true" : "false") << endl;
    return 0;
}
            

Solution in Python

def is_prime(n):
    if n <= 1:
        return False
    if n == 2:
        return True
    if n % 2 == 0:
        return False
    for i in range(3, int(n ** 0.5) + 1, 2):
        if n % i == 0:
            return False
    return True

n = 17
print(is_prime(n))
            

Explanation

Approach: Check divisibility up to the square root of n, optimizing by skipping even numbers after 2. Time Complexity: O(√n), Space Complexity: O(1).

Relevance: Prime number checks are a frequent GenC question testing number theory basics.

Question 4: Merge Two Sorted Arrays

Given two sorted arrays, merge them into a single sorted array.

Input: arr1 = [1, 3, 5], arr2 = [2, 4, 6]
Output: [1, 2, 3, 4, 5, 6]

Solution in C++

#include 
using namespace std;

vector mergeArrays(vector& arr1, vector& arr2) {
    int n1 = arr1.size(), n2 = arr2.size();
    vector result(n1 + n2);
    int i = 0, j = 0, k = 0;
    while (i < n1 && j < n2) {
        if (arr1[i] <= arr2[j]) result[k++] = arr1[i++];
        else result[k++] = arr2[j++];
    }
    while (i < n1) result[k++] = arr1[i++];
    while (j < n2) result[k++] = arr2[j++];
    return result;
}

int main() {
    vector arr1 = {1, 3, 5}, arr2 = {2, 4, 6};
    vector merged = mergeArrays(arr1, arr2);
    for (int x : merged) cout << x << " ";
    cout << endl;
    return 0;
}
            

Solution in Python

def merge_arrays(arr1, arr2):
    result = []
    i = j = 0
    while i < len(arr1) and j < len(arr2):
        if arr1[i] <= arr2[j]:
            result.append(arr1[i])
            i += 1
        else:
            result.append(arr2[j])
            j += 1
    result.extend(arr1[i:])
    result.extend(arr2[j:])
    return result

arr1, arr2 = [1, 3, 5], [2, 4, 6]
print(merge_arrays(arr1, arr2))
            

Explanation

Approach: Use two pointers to compare elements from both arrays, adding the smaller one to the result. Append remaining elements. Time Complexity: O(n1 + n2), Space Complexity: O(n1 + n2).

Relevance: Merging sorted arrays tests pointer techniques, a GenC staple.

Boost your Cognizant GenC prep with more coding challenges and expert tips! Stay ahead with our comprehensive resources.

Dive into More Placement Prep at JobsAddaa