Coding round cognizant Technical Interview questions
Coding round cognizant Technical Interview questions
Cognizant
GENC
Write a function that takes a string as input and returns the string reversed.
sl = arr[i];
}
i++;
}
if(sl==INT_MIN)
return -1;
return sl;
}
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.
Given a non-empty array of integers nums, every element appears twice except for one. Find
that single one.
return res;
}
GENC PRO
Find all duplicate in an array. Given an array of integers where each element may appear twice
or more, find all the duplicate elements.
return res;
}
Merge two sorted arrays. Write a program to merge two sorted arrays into one sorted array
without using any sorting function.
Check if two strings are Anagrams. Given two strings, write a function to check if they are
anagrams of each other.
Note: You can assume both the strings s1 & s2 are non-empty.
Examples :
Input: s1 = "listen", s2 = "silent"
Output: true
Explanation: Both the string have same characters with
same frequency. So, they are anagrams.
Input: s1 = "allergy", s2 = "allergic"
Output: false
Explanation: Characters in both the strings are not
same, so they are not anagrams.
class Solution
{
public:
//Function is to check whether two strings are anagram of each other or not.
bool isAnagram(string a, string b){
if (a[i] || b[i])
return false;
for(i=0;i<256;i++)
if (ca[i]!=cb[i])
return false;
return true;
};
Implement binary search in a sorted array.
Examples:
Input: arr1[] = {-5, 3, 6, 12, 15}, arr2[] = {-12, -10, -6, -3, 4, 10}
Output: The median is 3.
Explanation: The merged array is arr3[] = {-12, -10, -6, -5 , -3, 3,
4, 6, 10, 12, 15}. So the median of the merged array is 3.
Input: arr1[] = {2, 3, 5, 8}, arr2[] = {10, 12, 14, 16, 18, 20}
Output: The median is 11.
Explanation : The merged array is arr3[] = {2, 3, 5, 8, 10, 12, 14,
16, 18, 20}. The total number of the elements are even, so there
are two middle elements.
Take the average between the two: (10 + 12) / 2 = 11
Input: arr1[] = {}, arr2[] = {2, 4, 5, 6}
Output: The median is 4.5
Explanation: The merges array is arr3[] = {2, 4, 5, 6}. The total
number of elements are even, so there are two middle elements.
Take the average between the two: (4 + 5) / 2 = 4.5
Illustration:
arr1[] = { -5, 3, 6, 12, 15 } , arr2[] = { -12, -10, -6, -3, 4, 10 }
After concatenating them in a third array : arr3[] = { -5, 3, 6, 12,
15, -12, -10, -6, -3, 4, 10}
Sort arr3[ ] = { -12, -10, -6, -5, -3, 3, 4, 6, 10, 12, 15 }
As the length of arr3 is odd, so the median is the middle element
=3
// Concatenate
vector<int> arr3(arr1.begin(), arr1.end());
arr3.insert(arr3.end(), arr2.begin(), arr2.end());
// Sort the concatenated array
sort(arr3.begin(), arr3.end());