Cognizant GenC Elevate Coding Questions – Set 1

Cognizant GenC Elevate Coding Questions – Set 1 (Intermediate)

Previously asked Cognizant GenC Elevate Coding Questions – Set 1 (Intermediate)

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

Question 1: Maximum Subarray Sum (Kadane’s Algorithm)

Given an array of integers, find the maximum sum of any contiguous subarray.

Input: arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
Output: 6 (Subarray: [4, -1, 2, 1])

Solution in C++

#include 
using namespace std;

int maxSubArray(vector& arr) {
    int maxSoFar = arr[0], currMax = arr[0];
    for (int i = 1; i < arr.size(); i++) {
        currMax = max(arr[i], currMax + arr[i]);
        maxSoFar = max(maxSoFar, currMax);
    }
    return maxSoFar;
}

int main() {
    vector arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
    cout << maxSubArray(arr) << endl;
    return 0;
}
            

Solution in Python

def max_subarray(arr):
    max_so_far = curr_max = arr[0]
    for num in arr[1:]:
        curr_max = max(num, curr_max + num)
        max_so_far = max(max_so_far, curr_max)
    return max_so_far

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

Explanation

Approach: Use Kadane’s Algorithm: track the maximum sum ending at the current position (`currMax`) and the overall maximum (`maxSoFar`). At each step, choose between starting anew or extending the subarray. Time Complexity: O(n), Space Complexity: O(1).

Relevance: Dynamic programming basics are a GenC Elevate staple.

Question 2: Two Sum Problem

Given an array of integers and a target sum, find two indices of elements that add up to the target.

Input: arr = [2, 7, 11, 15], target = 9
Output: [0, 1] (2 + 7 = 9)

Solution in C++

#include 
using namespace std;

vector twoSum(vector& arr, int target) {
    unordered_map mp;
    for (int i = 0; i < arr.size(); i++) {
        int complement = target - arr[i];
        if (mp.find(complement) != mp.end()) {
            return {mp[complement], i};
        }
        mp[arr[i]] = i;
    }
    return {};
}

int main() {
    vector arr = {2, 7, 11, 15};
    int target = 9;
    vector result = twoSum(arr, target);
    for (int x : result) cout << x << " ";
    cout << endl;
    return 0;
}
            

Solution in Python

def two_sum(arr, target):
    seen = {}
    for i, num in enumerate(arr):
        complement = target - num
        if complement in seen:
            return [seen[complement], i]
        seen[num] = i
    return []

arr = [2, 7, 11, 15]
target = 9
print(two_sum(arr, target))
            

Explanation

Approach: Use a hash map to store numbers and their indices. For each number, check if its complement (target - num) exists. Time Complexity: O(n), Space Complexity: O(n).

Relevance: Hashing and two-pointer alternatives are common in GenC Elevate.

Question 3: Reverse Words in a String

Given a string, reverse the order of words without using extra space for the entire string.

Input: s = "Hello World Cognizant"
Output: "Cognizant World Hello"

Solution in C++

#include 
using namespace std;

void reverseWords(string& s) {
    int n = s.size(), start = 0;
    // Reverse entire string
    reverse(s.begin(), s.end());
    // Reverse each word
    for (int i = 0; i < n; i++) {
        if (s[i] == ' ') {
            reverse(s.begin() + start, s.begin() + i);
            start = i + 1;
        }
    }
    reverse(s.begin() + start, s.end());
}

int main() {
    string s = "Hello World Cognizant";
    reverseWords(s);
    cout << s << endl;
    return 0;
}
            

Solution in Python

def reverse_words(s):
    # Convert to list for in-place manipulation
    s_list = list(s)
    n = len(s_list)
    # Reverse entire string
    s_list[:] = s_list[::-1]
    start = 0
    for i in range(n):
        if s_list[i] == ' ':
            s_list[start:i] = s_list[start:i][::-1]
            start = i + 1
    s_list[start:] = s_list[start:][::-1]
    return ''.join(s_list)

s = "Hello World Cognizant"
print(reverse_words(s))
            

Explanation

Approach: Reverse the entire string, then reverse each word individually using space as a delimiter. Time Complexity: O(n), Space Complexity: O(1) for C++, O(n) for Python due to list conversion.

Relevance: String manipulation with in-place logic is a GenC Elevate favorite.

Question 4: Count Triplets with Sum Less Than Target

Given an array of integers and a target, count the number of triplets whose sum is less than the target.

Input: arr = [-2, 0, 1, 3], target = 2
Output: 2 (Triplets: [-2, 0, 1], [-2, 0, 3])

Solution in C++

#include 
using namespace std;

int countTriplets(vector& arr, int target) {
    int n = arr.size(), count = 0;
    sort(arr.begin(), arr.end());
    for (int i = 0; i < n-2; i++) {
        int left = i + 1, right = n - 1;
        while (left < right) {
            int sum = arr[i] + arr[left] + arr[right];
            if (sum < target) {
                count += (right - left);
                left++;
            } else {
                right--;
            }
        }
    }
    return count;
}

int main() {
    vector arr = {-2, 0, 1, 3};
    int target = 2;
    cout << countTriplets(arr, target) << endl;
    return 0;
}
            

Solution in Python

def count_triplets(arr, target):
    arr.sort()
    n, count = len(arr), 0
    for i in range(n-2):
        left, right = i + 1, n - 1
        while left < right:
            sum_val = arr[i] + arr[left] + arr[right]
            if sum_val < target:
                count += (right - left)
                left += 1
            else:
                right -= 1
    return count

arr = [-2, 0, 1, 3]
target = 2
print(count_triplets(arr, target))
            

Explanation

Approach: Sort the array, then use two pointers for each fixed first element. If the sum is less than the target, all elements between left and right form valid triplets. Time Complexity: O(n^2), Space Complexity: O(1).

Relevance: Two-pointer techniques and triplet problems are typical in GenC Elevate.

Elevate your Cognizant GenC skills with more intermediate challenges and insider tips! Explore our full range of resources.

Level Up with Jobs Addaa Placement Prep

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