0% found this document useful (0 votes)
42 views

Coding round cognizant Technical Interview questions

Uploaded by

subhash.chandra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Coding round cognizant Technical Interview questions

Uploaded by

subhash.chandra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Coding Round Questions

Cognizant

GENC

 Write a function that takes a string as input and returns the string reversed.

string reverseString(string str)


{
// Write your code here.
int l=str.length();
int lo=0,h=l-1;
while(lo<=h)
{
swap(str[lo],str[h]);
lo++;
h--;
}
return str;
}
 Write a program to check if a given string is a palindrome.

bool checkPalindrome(char str[]) {


// Write your code here
int l=0;
while(str[l++]);
int lo=0,h=l-2;
while(lo<=h)
{
if(str[lo]!=str[h])
return false;
lo++;
h--;
}
return true;
}
 Find second largest element in an array. Given an array of integers, write a function to find a
second largest element without sorting the array

int findSecondLargest(int n, vector<int> &arr)


{
// Write your code here.
int l=INT_MIN,sl=-1,i=0;
while(i<arr.size())
{
if (l < arr[i]) {
sl = l;
l = arr[i];
} else if (arr[i] < l && arr[i] > sl) {

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.

int missingNumber(vector<int>&a, int N) {


// Write your code here.
int sum=0;
for(int i=0;i<a.size();i++)
sum=sum+a[i];
return (N*(N+1)/2)-sum;
}

 Given a non-empty array of integers nums, every element appears twice except for one. Find
that single one.

int findSingle(const vector<int>& arr) {


unordered_map<int, int> freq;

// Store the frequency of each element


for (int num : arr) {
freq[num]++;
}

// Find and return the element that


// appears only once
for (const auto& entry : freq) {
if (entry.second == 1) {
return entry.first;
}
}
// If no single element is found, return -1
return -1;
}

int findSingle(const vector<int>& arr) {

// XOR all elements and return the result


int res = arr[0];
for (int i = 1; i < arr.size(); i++)
res ^= arr[i];

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.

vector<int> findDuplicates(vector<int>& arr) {

// Find frequency of every element


unordered_map<int, int> freq;
for (int x : arr)
freq[x]++;

// Move all those elements to resul that


// have frequency more than 1.
vector<int> res;
for (auto& entry : freq) {
if (entry.second > 1)
res.push_back(entry.first);
}

return res;
}

 Merge two sorted arrays. Write a program to merge two sorted arrays into one sorted array
without using any sorting function.

void mergeArrays(vector<int>& ar1, vector<int>& ar2,


vector<int>& ar3) {
int i = 0, j = 0, k = 0;
int n1 = ar1.size();
int n2 = ar2.size();

while (i < n1 && j < n2) {

// Pick smaller of the two current


// elements and move ahead in the
// array of the picked element
if (ar1[i] < ar2[j])
ar3[k++] = ar1[i++];
else
ar3[k++] = ar2[j++];
}

// if there are remaining elements of


// the first array, move them
while (i < n1)
ar3[k++] = ar1[i++];

// Else if there are remaining elements of


// the second array, move them
while (j < n2)
ar3[k++] = ar2[j++];
}

 Check if two strings are Anagrams. Given two strings, write a function to check if they are
anagrams of each other.

Given two strings s1 and s2 consisting of lowercase characters.


The task is to check whether two given strings are an anagram of
each other or not. An anagram of a string is another string that
contains the same characters, only the order of characters can be
different. For example, act and tac are an anagram of each other.
Strings s1 and s2 can only contain lowercase alphabets.

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.

Input: s1 = "g", s2 = "g"


Output: true
Explanation: Character in both the strings are same, so
they are anagrams.

class Solution
{
public:
//Function is to check whether two strings are anagram of each other or not.
bool isAnagram(string a, string b){

// Your code here


int ca[256]={0};
int cb[256]={0};
int i;
for(i=0;a[i] && b[i];i++)
{
ca[a[i]]++;
cb[b[i]]++;
}

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.

int BinarySearch(int nums[], int target, int length) {


//TODO: Write - Your - Code
int l=0,h=length-1,mid;
while(l<=h)
{
mid=l+((h-l)/2);
if(nums[mid]==target)
return mid;
if(target>nums[mid])
l=mid+1;
if(target<nums[mid])
h=mid-1;
}
return -1;
}
GENC NEXT

 Reverse a single linked list

//User function Template for C

struct Node* reverseList(struct Node *head)


{
struct Node *cr=head,*pr=NULL,*ne=NULL;
while(cr!=NULL)
{
ne=cr->next;
cr->next=pr;
pr=cr;
cr=ne;
}
head=pr;
return (head);
}
 Find the Median of two sorted arrays

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

double medianOf2(vector<int>& arr1, vector<int>& arr2) {

// 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());

// Calculate and return the median


int n = arr3.size();

// If length of array is even


if (n % 2 == 0) {
int mid1 = n / 2;
int mid2 = mid1 - 1;
return (arr3[mid1] + arr3[mid2]) / 2.0;
}

// If length of array is odd


else {
return arr3[n / 2];
}
}

 Find the K-th largest element in an unsorted array

int kthSmallest(int arr[], int N, int K)


{
// Sort the given array
sort(arr, arr + N);

// Return k'th element in the sorted array


return arr[K - 1];
}
Time Complexity: O(N log N)
Auxiliary Space: O(1)

You might also like