Previously Asked Cognizant GenC Coding Questions – Set 2 (Beginner to Intermediate)
These coding questions are crafted 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: Rotate Array by K Positions
Given an array of integers and a number k, rotate the array to the right by k positions.
Input: arr = [1, 2, 3, 4, 5], k = 2 Output: [4, 5, 1, 2, 3]
Solution in C++
#includeusing namespace std; void rotateArray(vector & arr, int k) { int n = arr.size(); k = k % n; // Handle k > n if (k == 0) return; reverse(arr.begin(), arr.begin() + n - k); reverse(arr.begin() + n - k, arr.end()); reverse(arr.begin(), arr.end()); } int main() { vector arr = {1, 2, 3, 4, 5}; int k = 2; rotateArray(arr, k); for (int x : arr) cout << x << " "; cout << endl; return 0; }
Solution in Python
def rotate_array(arr, k): n = len(arr) k = k % n # Handle k > n if k == 0: return arr[:] = arr[n-k:] + arr[:n-k] arr = [1, 2, 3, 4, 5] k = 2 rotate_array(arr, k) print(arr)
Explanation
Approach: In C++, use the reversal method: reverse the last k elements, then the first n-k, then the whole array. In Python, use slicing for simplicity. Time Complexity: O(n) for both, Space Complexity: O(1) for C++, O(n) for Python due to slicing.
Relevance: Array rotation is a common GenC question testing manipulation skills.
Question 2: Remove Duplicates from Sorted Array
Given a sorted array, remove duplicates in-place and return the new length.
Input: arr = [1, 1, 2, 3, 3, 4] Output: 4 (arr becomes [1, 2, 3, 4])
Solution in C++
#includeusing namespace std; int removeDuplicates(vector & arr) { if (arr.empty()) return 0; int writeIndex = 1; for (int i = 1; i < arr.size(); i++) { if (arr[i] != arr[i-1]) { arr[writeIndex] = arr[i]; writeIndex++; } } return writeIndex; } int main() { vector arr = {1, 1, 2, 3, 3, 4}; int newLength = removeDuplicates(arr); for (int i = 0; i < newLength; i++) cout << arr[i] << " "; cout << endl; return 0; }
Solution in Python
def remove_duplicates(arr): if not arr: return 0 write_index = 1 for i in range(1, len(arr)): if arr[i] != arr[i-1]: arr[write_index] = arr[i] write_index += 1 return write_index arr = [1, 1, 2, 3, 3, 4] new_length = remove_duplicates(arr) print(arr[:new_length])
Explanation
Approach: Use a pointer to track the position for unique elements. Since the array is sorted, compare adjacent elements and move unique ones forward. Time Complexity: O(n), Space Complexity: O(1).
Relevance: In-place array modification is a frequent GenC topic.
Question 3: Count Vowels in a String
Given a string, count the number of vowels (a, e, i, o, u) in it. Ignore case.
Input: s = "Hello World" Output: 3 (e, o, o)
Solution in C++
#includeusing namespace std; int countVowels(string s) { int count = 0; for (char c : s) { c = tolower(c); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { count++; } } return count; } int main() { string s = "Hello World"; cout << countVowels(s) << endl; return 0; }
Solution in Python
def count_vowels(s): vowels = set('aeiouAEIOU') return sum(1 for char in s if char in vowels) s = "Hello World" print(count_vowels(s))
Explanation
Approach: Iterate through the string, check each character against vowels (case-insensitive). C++ uses a condition, Python uses a set for efficiency. Time Complexity: O(n), Space Complexity: O(1).
Relevance: String traversal and character checking are staples in GenC exams.
Question 4: Find GCD of Two Numbers
Given two positive integers, find their Greatest Common Divisor (GCD) using the Euclidean algorithm.
Input: a = 48, b = 18 Output: 6
Solution in C++
#includeusing namespace std; int gcd(int a, int b) { while (b != 0) { int temp = b; b = a % b; a = temp; } return a; } int main() { int a = 48, b = 18; cout << gcd(a, b) << endl; return 0; }
Solution in Python
def gcd(a, b): while b: a, b = b, a % b return a a, b = 48, 18 print(gcd(a, b))
Explanation
Approach: Use the Euclidean algorithm: repeatedly divide and take the remainder until one number becomes 0. The other is the GCD. Time Complexity: O(log(min(a,b))), Space Complexity: O(1).
Relevance: Mathematical logic questions like GCD are common in GenC coding rounds.
Ready to ace your Cognizant GenC preparation? Explore more practice questions, tips, and resources to boost your skills!
Visit JobsAddaa for More Placement Prep MaterialsDiscover more from Jobs Addaa
Subscribe to get the latest posts sent to your email.