Cognizant Coding Questions 2024
Cognizant Common Coding Questions
1. Minimum Sum
Problem: You are given two integer arrays A and B of length N on which you have to perform below operation: In one operation, you can swap any two elements of 'A' or any two elements of 'B'
Your task is to find and return an integer value representing the minimum possible sum of A[i]*B[i] after performing the above operation any number of times. Note: The operation can also be performed 0 number of times.
Input Specification:
- input1: An integer value N representing the size of arrays.
- input2: An integer array A
- input3: An integer array B
Output Specification: Return an integer value representing the minimum possible sum of A[i]*B[i] after performing the above operation any number of times.
Example 1:
- input1: 4
- input2: {1,4,1,6}
- input3: {1,4,3,4}
- Output: 25
Explanation: Here A = {1,4,3,2} and B = {1,4,3,4}. To minimize the sum, we can swap the first two elements of A i.e., 4 and 1. The array will now become (4,1,3,2). The sum obtained will be 25, which is the minimum. Hence, 25 is returned as the output.
Example 2:
- input1: 3
- input2: (4,1,6)
- input3: (3,1,2)
- Output: 17
Explanation: Here, A = (4,1,6) and B= (3,1,2). To minimize the sum, we can swap the first two elements of A. i.e., 4 and 1 and the last two elements of B i.e., 1 and 2. The array A and B will now become (1,4,6) and (3,2,1) respectively. The sum obtained will be 17, which is the minimum. Hence, 17 is returned as the output.
2. Magical Library
Problem: In a magical library, each bookshelf is represented by a two-dimensional array A, where each row of the 2D array A[i] represents the series value of a book.
A row is considered magical if the sum of the odd values of the series of a book is even. Your task is to find and return an integer value representing the number of magical rows.
Input Specification:
- input1: An integer value representing the number of rows in the 2D array.
- input2: An integer value representing the number of columns in the 2D array.
- input3: A 2D integer array where each row represents a series of books.
Output Specification:
Return an integer value representing the number of magical rows.
Example 1:
- input1: 3
- input2: 3
- input3: ((1, 2, 3), (4, 5, 6), (7, 8, 9))
- Output: 2
Explanation: Here, the given 2D array is {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
- In the first row {1, 2, 3} the odd numbers are {1, 3} and their sum is 4 which is even.
- In the second row {4, 5, 6} the odd numbers are {5} and as there is only one odd element so the sum is 5 which is odd.
- In the third row {7, 8, 9} the odd numbers are {7, 9} and their sum is 16 which is even.
Therefore, there are only 2 magical rows so, 2 is returned as the output.
Example 2:
- input1: 3
- input2: 2
- input3: {{2, 4}, {0, 0}, {11, 11}}
- Output: 1
Explanation: Here, the given 2D given array {{2, 4}, {0, 0}, {11, 11}}, Only the last row {11, 11} has odd elements and their sum is 22 which is even. Therefore, there is only 1 magical row so, 1 is returned as the output.
3. Knowledge Enhancement
Problem: Alex is a high school student who loves reading and has a summer break coming up. He has a list of books he wants to read, with each book's estimated reading time stored in an array A. Alex has N hours available during the break for reading. Your task is to help Alex determine the maximum number of books he can read without exceeding his total available reading hours.
Input Specification:
- input1: An integer array A, where each element represents the estimated time to read each book.
- input2: An integer N, representing the total number of hours Alex has available for reading.
- input3: An integer size, representing the size of the array A.
Output Specification: Return an integer value representing the maximum number of books Alex can read without exceeding his total available reading hours.
Example 1:
- input1: [4, 2, 3, 1]
- input2: 5
- input3: 4
- Output: 2
Explanation: Here N=5 and Alex has 4 books with reading times of 4, 2, 3, and 1 respectively then
- The optimal way to utilize the 5 hours is to read the books with reading times of 2 and 1 hour.
- If he starts reading the book within 3 hours, then it will exceed the time limit. The maximum number of books that can be read is 2.
Hence, 2 is returned as output.
4. The Distance
Problem: Jim has a password represented by a string S consisting of lowercase English letters (a-z) and digits (0-9). The distance between two characters is defined as the absolute difference between their indices in the string. Your task is to find and return the maximum distance between two non-similar characters within the given password S.
Note: The distance between two adjacent characters is 1.
Input Specification:
- input1: A string S containing lowercase English letters (a-z) and digits (0-9).
Output Specification: Return an integer representing the maximum distance between two non-similar characters within the given password S.
Example 1:
- Input: abc10
- Output: 4
Explanation: In the string abc10, the maximum distances between non-similar characters are:
- Between a and 0 (indices 0 and 4): Distance is 4.
- Between b and 0 (indices 1 and 4): Distance is 3.
Other combinations yield a shorter distance. Therefore, the maximum distance between two non-similar characters is 4.
Example 2:
- Input: bbbb
- Output: 0
Explanation: In the string bbbb, all characters are the same, so there are no two non-similar characters to compare. Thus, the maximum distance between two non-similar characters is 0.
5. Charity Event
Problem: You are organizing a charity event in a village, where you distribute chocolates to children sitting in a circle. While the distribution of chocolates follows a specific set of rules based on their position in the circle:
The ith child receives i chocolates. If a child's position is adjacent to a multiple of 5, they receive an additional 2 chocolates.
Given the number of children in circle, your task is to calculate and return an integer value representing the total number of chocolates distributed.
Note: Return mod of total to manage overflow with 1e9+7.
Input Specification:
- input1: An integer value N, representing the number of children.
Output Specification: Return an integer value representing the total number of chocolates distributed.
Example 1:
- input1: 5
- Output: 19
Explanation: Here N=5, so the chocolates each child receives while sitting in the circle is:
- Child 1: 1 (since position is 1) and +2 (Additional gift), since they are sitting in circle child 5 will be adjacent to child 1. So child 1 gets 3 chocolates.
- Child 2: 2 (since position 2) chocolates
- Child 3: 3 chocolates.
- Child 4: 6 chocolates (4 base + 2 additional)
- Child 5: 5 chocolates.
Adding chocolates of each child will give us 19 (3+2+3+6+5). Hence, 19 is returned as output.
Example 2:
- Input1: 3
- Output: 6
6. Red Pen Green Pen
Problem: You are a teacher creating an engaging math activity for your students by writing N numbers on the classroom whiteboard. You use a green pen for odd numbers and a red pen for even numbers. Your task is to find and return an integer value representing the number of times you need to switch from the green pen to the red pen while writing these numbers.
Input Specification:
- input1: An integer value N
- input2: An integer array representing the numbers to be written
Output Specification: Return an integer value representing the number of times you need to switch from the green pen to the red pen while writing these numbers.
Example 1:
- input1: 5
- Input2: (1,2,1,6,10,9)
- Output: 2
Explanation: The given sequence which he has to write is 1->2->1->6->10->9 Below is the sequence of pen, the teacher has to use:
- Green pen to write 1
- Red pen to write 2
- Green pen to write 1
- Red pen to write 6 and 10
- Green pen to write 9
Therefore, we need to change from green pen to red twice. Hence, 2 is returned as output.
Example 2:
- input1: 6
- input2: (70,23,13,26,72,19)
- Output: 1
Explanation: The given sequence which he has to write is 70->23->13->26->72->19
7. Library Exploration
Problem: There is a library having N shelves in it. Each shelf is labeled from 1 to N and is stocked with A[i] books on each shelf. You have to select books only from shelves that have prime-numbered labels. Additionally, there is a limit of K books that you can select from each shelf.
Your task is to find and return an integer value representing the maximum number of books you can collect during a single visit to the library.
Note: Assume 1-based indexing.
Input Specification:
- input1: An integer value N representing the number of shelves in the library.
- input2: An integer value K representing the maximum number of books you can collect from each shelf.
- input3: An integer array A representing the number of books on each shelf.
Output Specification: Return an integer value representing the maximum number of books that you can collect during a single visit to the library.
Example 1:
- input1: 2
- input2: 4
- input3: [10, 2]
- Output: 2
Explanation:
Here, the given value of K is 4, which is the maximum number of books we can select from each shelf. Since we only have to select books from prime-numbered shelves, that is the shelf with index 2, and this shelf contains 2 books. So, we collect all books from it. Therefore, 2 is returned as the output.
Example 2:
- input1: 4
- input2: 5
- input3: [3, 7, 5, 6]
- Output: 10
8. Generated Numbers
Problem: You have a jar that initially contains N marbles. You can perform the following operations in any order and any number of times:
- Remove A marbles from the jar.
- Remove B marbles from the jar.
Your task is to find and return the number of unique positive values that can represent the number of marbles left in the jar after performing these operations. The initial number of marbles N should also be included in the count of unique values.
Note: The jar should never become empty during the operations.
Input Specification:
- input1: An integer value N, representing the initial number of marbles.
- input2: An integer value A, the number of marbles to be removed in the first operation.
- input3: An integer value B, the number of marbles to be removed in the second operation.
Output Specification: Return an integer representing the number of unique positive values for the number of marbles left in the jar.
Example 1:
- Input: N = 4, A = 1, B = 2
- Output: 4
Explanation: Starting with 4 marbles:
- Removing 1 marble leaves 3 marbles.
- Removing 2 marbles leaves 2 marbles.
- Removing 1 marble, then 2 marbles, leaves 1 marble.
- The initial count of 4 marbles is also considered.
Unique values: {1, 2, 3, 4}. Therefore, the output is 4.
Example 2:
- Input: N = 10, A = 2, B = 5
- Output: 8
Explanation: Starting with 10 marbles:
- Removing 2 marbles leaves 8 marbles.
- Removing 5 marbles leaves 5 marbles.
- Removing 2 marbles from 8 marbles leaves 6 marbles.
- Removing 5 marbles from 6 marbles leaves 1 marble.
- Removing 2 marbles from 6 marbles leaves 4 marbles.
- Removing 5 marbles from 10 marbles leaves 5 marbles.
- Removing 2 marbles from 4 marbles leaves 2 marbles.
Unique values: {1, 2, 3, 4, 5, 6, 8, 10}. Therefore, the output is 8.
9. Cryptographer
Problem: An ancient journal was found containing an encrypted message. The encryption used in the journal shifts each character one position forward in the alphabet. For example 'a' becomes 'b', 'b' becomes 'c', and so on. However, 'z' wraps around and becomes 'a'. Alex, a cryptographer, needs to decrypt this journal by reversing the shift. Your task is to return the decrypted string by shifting each character one position back in the alphabet. For example:
- The letter 'b' becomes 'a', 'c' becomes 'b', and so on.
- If the letter is 'a', it becomes 'z'.
Note: The journal contains only lowercase English letters.
Input Specification:
- input1: A string value consisting of lowercase English letters
Output Specification: Return the decrypted string after shifting all the characters one position back
Example 1:
- input1: bcd
- Output: abc
10. Sort String
Problem: You are a librarian in a bustling library where books are arranged in a single line, each represented by a letter from A to Z. One day in a hurry you mistakenly arranged some books out of order. Given a string S of length N representing the books, your task is to find and return an integer value representing the minimum number of books (indices in the string) that need to be moved to sort the row alphabetically.
Input Specification:
- input1: An integer value N representing the length of string.
- input2: A string S representing the current order of the books.
Output Specification: Return an integer representing the minimum number of books (indices in the string) that needs to be moved to sort the row alphabetically.
Example 1:
- input1: 5
- input2: helco
- Output: 3
Explanation: The given string is "helco". When the string is sorted, it becomes "cehlo". So, to sort the given string, characters at the indices 0, 2, and 3 are needed to be rearranged. Since there are 3 indices whose characters need to be rearranged to sort the string 3 is returned as the output.
Example 2:
- input1: 4
- input2: asdf
- Output: 3
Explanation: The given string is "asdf". When the string is sorted, it becomes "adfs". So, to sort the given string, characters at indices 1, 2, and 3 needs to be rearranged. Since there are 3 indices whose characters need to be rearranged to sort the string, 3 is returned as the output.
11. Discounted Sum
Problem: You are given an array of integers and an integer n. Your task is to find the sum of the first n largest unique elements from the array. Subtract the largest element (from those n elements) from the sum (this is called a discount). Finally, return the result after applying the discount.
Input:
- arr (List of integers): The array of integers.
- n (Integer): The number of largest elements to sum.
Output: The resultant value after subtracting the largest element from the sum of the first n largest elements.
Constraints:
- 1 ≤ n ≤ length of arr.
- If n is greater than the number of unique elements, the output should be 0.
Example 1:
- Input: arr = [5, 2, 9, 1, 7, 4, 6], n = 3
- Output: 13
Explanation: The largest unique elements are [9, 7, 6]. Sum of these is 9 + 7 + 6 = 22. After subtracting the largest element (9), the result is 22 - 9 = 13.
Example 2:
- Input: arr = [5, 2, 9, 1, 7, 4, 6], n = 1
- Output: 0
Explanation: The largest unique element is 9. After subtracting it from itself, the result is 9 - 9 = 0.
12. Valid Palindromes
Problem: You are given a string s containing blanks ('_') and an integer n. Your task is to count the total number of ways to fill the blanks such that the resultant string becomes a valid palindrome. Since the result can be large, return the answer modulo n.
Input:
- s (String): The string containing blanks and other characters.
- n (Integer): The modulus value.
Output: Return the number of valid palindromes that can be formed by filling the blanks, modulo n.
Constraints:
- 1 <= |s| <= 10^5 (Length of the string).
- The string contains only lowercase alphabets and '_' representing blanks.
Example 1:
- Input: s = "a_b_a", n = 1000000007
- Output: 26
Explanation: The string is "a_b_a". To make this a palindrome, we can replace the blank with any letter (b), so there are 26 possible valid palindromes.
Example 2:
- Input: s = "a\_a_", n = 1000000007
- Output: 676
Explanation: The string is "a\_a_". The two blanks at the start and the end can each be filled with any letter (26 choices). The middle blanks must match their corresponding characters to form a palindrome. Hence, the total possible palindromes are 26 * 26 = 676.
13. Wave Theater Bill
Problem: Vohra went to a movie with his friends in a Wave theater and during break time he bought pizzas, puffs and cool drinks. Consider the following prices:
- Rs.100/pizza
- Rs.20/puffs
- Rs.10/cooldrink
Generate a bill for what Vohra has bought.
Sample Input 1:
Enter the no of pizzas bought: 10
Enter the no of puffs bought: 12
Enter the no of cool drinks bought: 5
Sample Output 1:
No of pizzas: 10
No of puffs: 12
No of cooldrinks: 5
Total price=1290
14. Palindrome Checker
Problem: Goutam and Tanul play by telling numbers. Goutam says a number to Tanul. Tanul should first reverse the number and check if it is the same as the original. If yes, Tanul should say "Palindrome". If not, he should say "Not a Palindrome". If the number is negative, print "Invalid Input". Help Tanul by writing a program.
Sample Input 1:
21212
Sample Output 1:
Palindrome
15. Count Words
Problem: Count the number of words in a string.
16. Sum of Prime Factors
Problem: Find the sum of prime factors of a number.
17. Mid Positive Element
Problem: Find the middle positive element in a given array.
GENC Coding Problems
1: Half Sum Array
Write a program that checks whether the sum of the first half of an array is less than the sum of the second half. If this condition is met, the program should reverse the entire array. Finally, the program should print the resulting array.
2: Count Valid Subarrays of Size 3
You are given an array of integers containing N elements. Your task is to find and return an integer value representing the total number of subarrays of size 3 where the sum of the first element and the third element is equal to the second element.
A subarray is a continuous sequence of elements in an array.
3: Generate a Bill for Vohra's Purchase
Vohra went to a movie with his friends and bought pizzas, puffs, and cool drinks during the break. Given the prices:
- Rs. 100 per pizza
- Rs. 20 per puff
- Rs. 10 per cooldrink
Generate a bill for what Vohra has bought.
Sample Input 1
Enter the number of pizzas bought: 10
Enter the number of puffs bought: 12
Enter the number of cool drinks bought: 5
Sample Output 1
Number of pizzas: 10
Number of puffs: 12
Number of cooldrinks: 5
Total price: 1290
4: Palindrome Check
Goutam says a number to Tanul. Tanul should first reverse the number and check if it is the same as the original. If yes, Tanul should say "Palindrome". If not, he should say "Not a Palindrome". If the number is negative, print "Invalid Input". Write a program to help Tanul.
Sample Input 1
21212
Sample Output 1
Palindrome
5: Maximum Subarray Sum
Given an integer array nums, find the subarray with the largest sum, and return its sum.
Input Example
nums = [-2,1,-3,4,-1,2,1,-5,4]
Output Example
6
6: Buzz Number Check
Implement a function to check whether a given number is a Buzz number or not. A Buzz number is a number that ends with the digit 7 or is divisible by 7.
Examples
- 42 is a Buzz number because it is divisible by 7.
- 107 is a Buzz number because it ends with 7.
- 147 is a Buzz number because it ends with 7 and is also divisible by 7.
7: ASCII Character Display
Ritik wants a magic board that displays a character for a corresponding number for his science project. Develop an application that converts given numbers to their corresponding characters.
Example: When the digits 65, 66, 67, 68 are entered, the alphabet A, B, C, D should be displayed.
Sample Input 1
Enter the digits:
65
66
67
68
Sample Output 1
65 - A
66 - B
67 - C
68 - D
Ignite Coding questions
1: Find the Second Largest Number in an Array
Write a function to find the second largest number in an array.
2: Merge Two Sorted Arrays
Write a function to merge two sorted arrays into one sorted array.
3: Reverse an Integer
Write a function to reverse the digits of a given integer.
4: Count the Number of Vowels in a String
Write a function to count the number of vowels in a given string.
5: Find LCM (Least Common Multiple)
Write a function to find the LCM of two numbers using the GCD.
6: Find GCD (Greatest Common Divisor) Using Recursion
Write a recursive function to find the GCD of two numbers.
7: Check if a Number is Prime
Write a function to check if a given number is prime.
8: Fibonacci Sequence (Recursive)
Write a recursive function to find the nth Fibonacci number.
9: Find the Minimum Element in an Array
Write a function to find the minimum element in an array.
10: Find the Maximum Element in an Array
Write a function to find the maximum element in an array.
11: Bubble Sort
Implement the bubble sort algorithm to sort an array of integers.
12: Binary Search
Implement a binary search algorithm to find a target value in a sorted array.
13: Find the Factorial of a Number
Write a function to calculate the factorial of a given number using recursion.
14: Check if a String is a Palindrome
Write a function to check if a given string is a palindrome.
15: Reverse a String
Write a function to reverse a given string.
Cognizant Common Coding Questions
1: Reverse a String
Write a function that takes a string as input and returns the string reversed.
2: Find the Second Largest Element in an Array
Given an array of integers, write a function to find the second largest element without sorting the array.
3: Check if a String is a Palindrome
Write a program to check if a given string is a palindrome.
4: Find the Missing Number in an Array
Given an array of numbers from 1 to n with one missing number, write a program to find the missing number.
5: Find All Duplicates in an Array
Given an array of integers where each element may appear twice or more, find all the duplicate elements.
6: Merge Two Sorted Arrays
Write a program to merge two sorted arrays into one sorted array without using any sorting function.
7: Reverse a Linked List
Reverse a singly linked list.
8: Median of Two Sorted Arrays
Find the median of two sorted arrays.
Join Our WhatsApp Channel for more resources