Sie sind auf Seite 1von 10

Cocubes Questions

Q
Second largest Number: CoCubes Coding Question

1. PROBLEM STATEMENT:
You are given a function,
int FindSecondLargestNumber(int a,int b,int c);
The function takes three integers 'a', 'b','c' as input .Implement the function to
find and return the second largest number.

CODING:
int FindSeconLargestNumber(int a,int b,int c);
{

Answer:
#include <stdio.h>

int main()

{
int a,b,c,lar,seclar;

scanf("%d%d%d",&a,&b,&c);

if(a>b)
{
if(a>c)
lar=a;
else
seclar=a;
}

if(b>c)
{
if(b>a)
lar=a;
else
seclar=b;
}
if(c>a)
{
if(c>b)
lar=c;
else
seclar=c;
}
printf("second largest number :%d",seclar);

}
Q
Reverse array : CoCubes coding question
Ques. You are given a function,int* ReverseArray(int* arr, int length);
The function takes an integer array and its length as input. Implement the function
to return the array such that the array is reversed i.e. the first element of the
array occupies the last position, second element occupies the second last position
and so on.

Note:
The re-arrangement is to be done in-place i.e you cannot use another array.

Assumption:
You may assume that the array is of even length.

Example:

Input:
2 4 6 8 20 15 10 5

Output:
5 10 15 20 8 6 4 2

*********************************************************************************
Program
*********************************************************************************

#include<stdio.h>

/* Function to reverse arr[] from start to end*/


void rvereseArray(int arr[], int start, int end)
{
int temp;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}

/* Utility that prints out an array on a line */


void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);

printf("\n");
}

/* Driver function to test above functions */


int main()
{
int arr[] = {1, 2, 3, 4, 5, 6};
printArray(arr, 6);
rvereseArray(arr, 0, 5);
printf("Reversed array is \n");
printArray(arr, 6);
return 0;
}

Q
Find sum leaving our row and col: CoCubes coding question

PROBLEM STATEMENT:
You are given a function,
int FindSumLeavingOutRowCol(int** arr, int m, int n, int i,int j);
The function takes a two-dimensional array 'arr', its number of rows 'm', its
number of columns 'n' and integers 'i' and 'j' as input. Implement the function to
find and return the sum of elements of the array leaving out the elements of the i
and j column. The algorithm to find the sum is as follows:
1.Iterate over every row except for i row,and keep on adding each element except
for the elements of j column to a variable 'sum'.
NOTE:
1.Row and column indices start from 0.
2. Value of i and j from 1.

CODING:
int FindSumLeavingOutRowCol(int** arr, int m, int n,int j);
{
/* write your code here */

***********************
Program
***********************

/*
* C program to accept a matrix of order M x N and find the sum
* of each row and each column of a matrix
*/
#include <stdio.h>

void main ()
{
static int array[10][10];
int i, j, m, n, sum = 0;

printf("Enter the order of the matrix\n");


scanf("%d %d", &m, &n);
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf("Sum of the %d row is = %d\n", i, sum);
sum = 0;
}
sum = 0;
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
sum = sum + array[i][j];
}
printf("Sum of the %d column is = %d\n", j, sum);
sum = 0;
}
}

Q!
Printing all the Leaders in an Array

Write a program to print all the LEADERS in the array. An element is leader if it
is greater than all the elements to its right side.

And the rightmost element is always a leader. For example int the array {16, 19, 4,
3, 8, 3}, leaders are 19, 8 and 3?
With C++

#include<iostream>
using namespace std;

/*C++ Function to print leaders in an array */


void printLeaders(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
int j;
for (j = i+1; j < size; j++)
{
if (arr[i] <= arr[j])
break;
}
if (j == size) // the loop didn’t break
cout << arr[i] << ” “;
}
}

/* Driver program to test above function */


int main()
{
int arr[] = {16, 17, 4, 3, 5, 2};
int n = sizeof(arr)/sizeof(arr[0]);
printLeaders(arr, n);
return 0;
}

Q2

Maximum difference between two elements such that larger element appears after the
smaller number

Given an array arr[] of integers, find out the difference between any two elements
such that larger element appears after the smaller number in arr[].

Examples: If array is [2, 3, 10, 6, 4, 8, 1] then returned value should be 8 (Diff


between 10 and 2). If array is [ 7, 9, 5, 6, 3, 2 ] then returned value should be 2
(Diff between 7 and 9)

Time Complexity: O(n^2)


Auxiliary Space: O(1)

Use two loops. In the outer loop, pick elements one by one and in the inner loop
calculate the difference of the picked element with every other element in the
array and compare the difference with the maximum difference calculated so far.

C
Java

#include

/* The function assumes that there are at least two


elements in array.
The function returns a negative value if the array is
sorted in decreasing order.
Returns 0 if elements are equal */
int maxDiff(int arr[], int arr_size)
{
int max_diff = arr[1] – arr[0];
int i, j;
for (i = 0; i < arr_size; i++)
{
for (j = i+1; j < arr_size; j++)
{
if (arr[j] – arr[i] > max_diff)
max_diff = arr[j] – arr[i];
}
}
return max_diff;
}

/* Driver program to test above function */


int main()
{
int arr[] = {1, 2, 90, 10, 110};
printf(“Maximum difference is %d”, maxDiff(arr, 5));
getchar();
return 0;
}

Q3

Longest Prefix Suffix

Given a string of character, find the length of longest proper prefix which is also
a proper suffix.
Example:
S = abab
lps is 2 because, ab.. is prefix and ..ab is also a suffix.

Input:
First line is T number of test cases. 1<=T<=100.
Each test case has one line denoting the string of length less than 100000.

Expected time compexity is O(N).

Output:
Print length of longest proper prefix which is also a proper suffix.

Example:
Input:
2
abab
aaaa

Output:
2
3
C++

#include < bits / stdc++.h >


using namespace std;
int lps(string);
int main() {
//code
int T;
cin >> T;
getchar();
while (T–) {
string s;
cin >> s;
printf(“%d\n”, lps(s));
}
return 0;
}
int lps(string s) {
int n = s.size();
int lps[n];
int i = 1, j = 0;
lps[0] = 0;
while (i < n) {
if (s[i] == s[j]) {
j++;
lps[i] = j;
i++;
} else {
if (j != 0)
j = lps[j – 1];
else {
lps[i] = 0;
i++;
}
}
}
return lps[n – 1];
}
Q4

Find the number closest to n and divisible by m

Given two integers n and m. The problem is to find the number closest to n and
divisible by m. If there are more than one such number, then output the one having
maximum absolute value. If n is completely divisible by m, then output n only. Time
complexity of O(1) is required.

Constraints: m != 0

We find value of n/m. Let this value be q. Then we find closest of two
possibilities. One is q * m other is (m * (q + 1)) or (m * (q – 1)) depending on
whether one of the given two numbers is negative or not.

Algorithm:

closestNumber(n, m)
Declare q, n1, n2
q = n / m
n1 = m * q

if (n * m) > 0
n2 = m * (q + 1)
else
n2 = m * (q - 1)

if abs(n-n1) < abs(n-n2)


return n1
return n2

C++

// C++ implementation to find the number closest to n


// and divisible by m
#include <bits/stdc++.h>

using namespace std;

// function to find the number closest to n


// and divisible by m
int closestNumber(int n, int m)
{
// find the quotient
int q = n / m;

// 1st possible closest number


int n1 = m * q;

// 2nd possible closest number


int n2 = (n * m) > 0 ? (m * (q + 1)) : (m * (q – 1));

// if true, then n1 is the required closest number


if (abs(n – n1) < abs(n – n2))
return n1;

// else n2 is the required closest number


return n2;
}

// Driver program to test above


int main()
{
int n = 13, m = 4;
cout << closestNumber(n, m) << endl;

n = -15; m = 6;
cout << closestNumber(n, m) << endl;

n = 0; m = 8;
cout << closestNumber(n, m) << endl;

n = 18; m = -7;
cout << closestNumber(n, m) << endl;

return 0;
}

Q5

Given a string consisting of only 0, 1, A, B, C where


A = AND
B = OR
C = XOR
Calculate the value of the string assuming no order of precedence and evaluation is
done from left to right.

Constraints – The length of string will be odd. It will always be a valid string.
Example, 1AA0 will not be given as an input.

Examples:

Input : 1A0B1
Output : 1
1 AND 0 OR 1 = 1

Input : 1C1B1B0A0
Output : 0

C/C++

// C++ program to evaluate value of an expression.


#include <bits/stdc++.h>
using namespace std;

int evaluateBoolExpr(string s)
{
int n = s.length();

// Traverse all operands by jumping


// a character after every iteration.
for (int i = 0; i < n; i += 2) {

// If operator next to current operand


// is AND.
if (s[i + 1] ==’A’) {
if (s[i + 2] ==’0’|| s[i] ==’0’)
s[i + 2] =’0’;
else
s[i + 2] =’1’;
}

// If operator next to current operand


// is OR.
else if (s[i + 1] ==’B’) {
if (s[i + 2] ==’1’|| s[i] ==’1’)
s[i + 2] =’1’;
else
s[i + 2] =’0’;
}

// If operator next to current operand


// is XOR (Assuming a valid input)
else {
if (s[i + 2] == s[i])
s[i + 2] =’0’;
else
s[i + 2] =’1’
}
}
return s[n – 1] -‘0’;
}

// Driver code
int main()
{
string s = “1C1B1B0A0”;
cout << evaluateBoolExpr(s);
return 0;
}

Q6
Ques. 1 Write a program to find out total marks obtained by a student if the
student gets 3 marks for the correct answer and -1 for the wrong answer?

Q7
You’re given a function –

char *CompressString(char* str);

The function accepts a string as an argument that may contain repetitive


characters. Implement the function to modify and return the input string, such that
each character once, along with the count of consecutive occurrence. Do not append
count if the character occurs only once.

Note –

The string will only contain lowercase English Alphabets


If you have to manipulate the input string in place you cant use another string

Assumption –

No character will occur consecutively more than 9 times.

Example –
input – aaaaabbbccccccccdaa

OutPut – a4b3c8da2

Q
You’re given a function,

char * ConvertToPalindrome(char* str)

The function accepts a string str, implement the function to find and return the
minimum characters required to append at the end of str to make it a palindrome

Assumptions –

The string will only contain lowercase English Alphabets

Note –

If string is already a palindrome then return NULL


You have to find the minimum characters required to append at the end of the
string to make it a palindrome

Example –

Input –

abcdc

Output –

ba

Das könnte Ihnen auch gefallen