Cocubes Coding Questions asked in Previous Papers

Reverse array : CoCubes coding question

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;
}


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);
{
/*write your code here .*/

}


********************
PROGRAM
*******************

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
***********************

No comments:

Post a Comment