Cognizant GenC Coding Questions 2025 – Set 4

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

Previously asked Cognizant GenC Coding Questions – Set 4 (Beginner to Intermediate)

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

Question 1: Find the Frequency of Elements in an Array

Given an array of integers, print the frequency of each element.

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

Solution in C++

#include 
using namespace std;

void printFrequencies(vector& arr) {
    unordered_map freq;
    for (int num : arr) freq[num]++;
    for (auto& pair : freq) {
        cout << pair.first << ": " << pair.second << endl;
    }
}

int main() {
    vector arr = {1, 2, 2, 3, 1, 4};
    printFrequencies(arr);
    return 0;
}
            

Solution in Python

from collections import Counter

def print_frequencies(arr):
    freq = Counter(arr)
    for num, count in freq.items():
        print(f"{num}: {count}")

arr = [1, 2, 2, 3, 1, 4]
print_frequencies(arr)
            

Explanation

Approach: Use a hash map (unordered_map in C++, Counter in Python) to count occurrences of each element. Iterate and print the results. Time Complexity: O(n), Space Complexity: O(k) where k is the number of unique elements.

Relevance: Frequency counting tests basic data structure usage, a common GenC topic.

Question 2: Check if Two Strings are Anagrams

Given two strings, check if they are anagrams (contain the same characters with the same frequencies).

Input: s1 = "listen", s2 = "silent"
Output: true

Solution in C++

#include 
using namespace std;

bool areAnagrams(string s1, string s2) {
    if (s1.size() != s2.size()) return false;
    int count[26] = {0};
    for (int i = 0; i < s1.size(); i++) {
        count[s1[i] - 'a']++;
        count[s2[i] - 'a']--;
    }
    for (int i = 0; i < 26; i++) {
        if (count[i] != 0) return false;
    }
    return true;
}

int main() {
    string s1 = "listen", s2 = "silent";
    cout << (areAnagrams(s1, s2) ? "true" : "false") << endl;
    return 0;
}
            

Solution in Python

def are_anagrams(s1, s2):
    if len(s1) != len(s2):
        return False
    return sorted(s1) == sorted(s2)

s1, s2 = "listen", "silent"
print(are_anagrams(s1, s2))
            

Explanation

Approach: In C++, use a frequency array to increment for s1 and decrement for s2; if all counts are 0, they’re anagrams. In Python, sort and compare (simpler but less efficient). Time Complexity: O(n) for C++, O(n log n) for Python, Space Complexity: O(1) for C++, O(n) for Python.

Relevance: Anagram checks test string manipulation, a frequent GenC question.

Question 3: Find Largest Element in an Array

Given an array of integers, find the largest element without using built-in functions like max().

Input: arr = [3, 7, 2, 9, 1]
Output: 9

Solution in C++

#include 
using namespace std;

int findLargest(vector& arr) {
    if (arr.empty()) return -1;
    int largest = arr[0];
    for (int i = 1; i < arr.size(); i++) {
        if (arr[i] > largest) largest = arr[i];
    }
    return largest;
}

int main() {
    vector arr = {3, 7, 2, 9, 1};
    cout << findLargest(arr) << endl;
    return 0;
}
            

Solution in Python

def find_largest(arr):
    if not arr:
        return -1
    largest = arr[0]
    for num in arr[1:]:
        if num > largest:
            largest = num
    return largest

arr = [3, 7, 2, 9, 1]
print(find_largest(arr))
            

Explanation

Approach: Initialize the largest as the first element, then iterate to update it if a larger element is found. Time Complexity: O(n), Space Complexity: O(1).

Relevance: Finding extremes in arrays is a basic GenC problem testing iteration skills.

Question 4: Factorial of a Number

Given a non-negative integer, compute its factorial.

Input: n = 5
Output: 120 (5 * 4 * 3 * 2 * 1 = 120)

Solution in C++

#include 
using namespace std;

long long factorial(int n) {
    if (n < 0) return -1;
    long long result = 1;
    for (int i = 1; i <= n; i++) {
        result *= i;
    }
    return result;
}

int main() {
    int n = 5;
    cout << factorial(n) << endl;
    return 0;
}
            

Solution in Python

def factorial(n):
    if n < 0:
        return -1
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result

n = 5
print(factorial(n))
            

Explanation

Approach: Multiply numbers from 1 to n iteratively. Use long long in C++ to handle larger values. Time Complexity: O(n), Space Complexity: O(1).

Relevance: Factorial computation tests loops and basic math, a classic GenC question.

Master your Cognizant GenC prep with additional coding problems and expert guidance! Keep learning and stay ahead.

Unlock More Placement Prep Resources at JobsAddaa