KEMBAR78
Coding Questions | PDF | Function (Mathematics) | Computer Science
0% found this document useful (0 votes)
12 views16 pages

Coding Questions

There are dsa questions given and errors need to be spotted and figured out
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views16 pages

Coding Questions

There are dsa questions given and errors need to be spotted and figured out
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

1.

Write a function manchester that processes an input array of bits (0 and 1) and prints a
space-separated sequence of integers according to the following rule:

 For the first bit in the array (index 0), assume the previous bit is 0. If the current bit is
equal to the assumed previous bit (i.e., arr[0] == 0), set a counter to 1 and output it;
otherwise, output 0.

 For every subsequent bit (i ≥ 1):

 If the current bit is equal to the previous bit (arr[i] == arr[i - 1]), increment the
counter and print its value.

 If the current bit is different, reset the counter to 0 and print 0.

#include <iostream>

using namespace std;

void manchester(int size, int arr[]) {

int res[size];

int count = 0;

// Handle first bit assuming previous bit is 0

if (arr[0] == 0)

count = 1;

res[0] = count;

// Process remaining bits

for (int i = 1; i < size; i++) {

if (arr[i] == arr[i - 1]) {

count++;

res[i] = count;

} else {

count = 0;
res[i] = 0;

// Print the output

for (int i = 0; i < size; i++) {

cout << res[i] << " ";

cout << endl;

int main() {

int arr[] = {0, 1, 0, 0, 1, 1, 1, 0};

int size = sizeof(arr) / sizeof(arr[0]);

manchester(size, arr);

return 0;

2. Write a program in C++ to print the first num numbers of the Fibonacci series.
For example, if num = 5, the output should be:
01123

class Solution {

public:

void printFibonacci(int num) {

long long num1 = 0;


long long num2 = 1;

for (int i = 1; i <= num; ++i) {

cout << num1 << " ";

long long sum = num1 + num2;

num1 = num2;

num2 = sum;

};

3. The function/method reverseHalfArray modify the input list by reversing the input list
from the second half. For example, if the inputList is [20, 30, 10, 40, 50], the
function/method is expected to modify the inputList like [20, 30, 50, 40, 10]. The
function/method reverseHalfArray accepts two arguments - size, an integer representing the
size of the list and } © 2025 SHL and/or its affiliates. All rights reserved.. 18/34 inputList, a
list of integers representing the given input list, respectively. The function/method compiles
successfully but fails to get the desired result for some test cases. Your task is to debug the
code so that it passes all the test cases.

#include <iostream>

using namespace std;

class Solution {

public:

void reverseHalfArray(int size, int inputList[]) {

int i, temp;

for(i = size / 2; i < size; i++) {

temp = inputList[size - 1];

inputList[size - 1] = inputList[i];

inputList[i] = temp;
size -= 1;

};

4. The function/method printPattern accepts an argument num, an integer. The


function/method printPattern print the first num lines of the pattern as shown below. For
example, if num = 3, the pattern should be: 1 1 2 2 2 2 3 3 3 3 3 3 The function/method
printPattern compiles successfully but fails to print the desired result for some test cases.
Your task is to debug the code so that it passes all the test cases.

#include <iostream>

using namespace std;

class Solution {

public:

void printPattern(int num) {

int i, j;

for (i = 1; i <= num; i++) {

for (j = 1; j <= i + i; j++) {

cout << i << " ";

cout << endl;

};

5. You are given predefined structure Time containing hour, minute, and second as
members. A collection of functions/methods for performing some common operations on
times is also available. You must make use of these } } } © 2025 SHL and/or its affiliates. All
rights reserved.. 21/34 / p g p functions/methods to calculate and return the difference. The
function/method difference_in_times accepts two arguments - time1, and time2,
representing two times and is supposed to return an integer representing the difference in
the number of seconds. You must complete the code so that it passes all the test cases

public:

int difference_in_times(Time time1, Time time2) {

int diff = 0;

// Ensure time1 is the smaller or equal time

if (time1.compareTo(time2) == 1) {

swap(time1, time2);

while (time1.compareTo(time2) != 0) {

time1.addSecond();

diff++;

return diff;

};

6. You are given a predefined structure Point and also a collection of related
functions/methods that can be used to perform some basic operations on the structure. You
must implement the function/method isTriangle which accepts three points P1, P2, P3 as
inputs and checks whether the given three points form a triangle. If they form a triangle, the
function/method returns an integer 1. Otherwise, it returns an integer 0

int isTriangle(Point P1, Point P2, Point P3) {

double a = P1.calculateDistance(P2);

double b = P2.calculateDistance(P3);

double c = P3.calculateDistance(P1);

cout << a << " " << b << " " << c << endl;
if ((a + b) > c && (b + c) > a && (c + a) > b) {

return 1;

} else {

return 0;

};

7. The function/method removeKLargestElement prints space-separated integers that are


left after removing else{ return 0; } } } © 2025 SHL and/or its affiliates. All rights reserved..
26/34 the K largest integer from the input list arr . If the given K is out of bounds ( K > len
) or K largest integer does not exist, then the function/method should print space-
separated integers of the input list arr . The function/method removeKLargestElement
accepts three arguments - len , an integer representing the number of elements in the list,
K , an integer value and arr , a list of integers. The function/method
removeKLargestElement uses another error-free function/method kLargestElement which
accepts three arguments - len, an integer representing the number of elements in the list,
K , an integer value and arr , a list of integers and returns an integer representing the K
largest integer in the input list arr . The function/method removeKLargestElement compiles
unsuccessfully due to syntactical error. Your task is to debug the code so that it passes all the
test cases

#include <iostream>

using namespace std;

class Solution

public:

int kLargestElement(int len, int K, int arr[])

int kth, temp;

int rarr[1000]; // assuming max len <= 1000


for (int i = 0; i < len; i++)

rarr[i] = arr[i];

for (int i1 = 0; i1 < len; i1++)

for (int j1 = i1 + 1; j1 < len; j1++)

if (rarr[i1] > rarr[j1])

temp = rarr[i1];

rarr[i1] = rarr[j1];

rarr[j1] = temp;

kth = rarr[len - K];

return kth;

void removeKLargestElement(int len, int K, int arr[])

if (K > 0 && K <= len)

int i, pos = 0;

int kth = kLargestElement(len, K, arr);


for (int j1 = 0; j1 < len; j1++)

if (arr[j1] == kth)

pos = j1;

for (i = pos; i < len - 1; i++)

arr[i] = arr[i + 1];

for (i = 0; i < len - 1; i++)

cout << arr[i];

if (i < len - 2)

cout << " ";

else

for (int i = 0; i < len; i++)

cout << arr[i];

if (i < len - 1)

cout << " ";


}

};

8. The function/method replaceValues accepts two arguments - size, an integer representing


the size of the given input list and inputList, a list of integers representing the input list. The
function/method replaceValues compiles successfully but fails to get the desired result for
some test cases due to incorrect implementation of the function. Your task is to fix the code
so that it passes all the test cases

void replaceValues(int size, int *inputList)

int i, j;

if (size % 2 == 0)

i = 0;

while (i < size)

inputList[i] = 0;

i++;

else

j = 0;

while (j < size)

inputList[j] = 1;
j++;

9. The function/method calculateMatrixSum returns an integer representing the sum of odd


elements of the given matrix whose i and j index are the same. The function/method
calculateMatrixSum accepts three arguments - rows, an integer representing the number of
rows of the given matrix, columns, an integer representing the number of columns of the
given matrix and matrix, representing a two-dimensional array of integers. The
function/method calculateMatrixSum compiles successfully but fails to return the desired
result for some test cases due to logical errors. Your task is to fix the code so that it passes all
the test cases.

using namespace std;

int calculateMatrixSum(int rows, int columns, int *matrix)

int i, j, sum = 0;

if ((rows > 0) && (columns > 0))

for (i = 0; i < rows; i++)

for (j = 0; j < columns; j++)

if (i == j)

int value = *((matrix + i * columns) + j); // Access matrix[i][j]

if (value % 2 == 0) // Check if even

sum += value;

}
}

return sum;

10 . The function/method maxReplace prints space separated integers representing the


input list after replacing all elements of the input list with the maximum element of the
input list. The function/method maxReplace accept two arguments - size, an integer
representing the size of the input list and inputList, a list of integers representing the input
list, respectively. The function/method maxReplace compiles unsuccessfully due to
compilation error. Your task is to debug the code so that it passes all the test cases

using namespace std;

void maxReplace(int size, int* inputList)

if (size > 0)

int max = inputList[0];

for (int i = 1; i < size; i++)

if (max < inputList[i])

max = inputList[i];

for (int i = 0; i < size; i++)

{
inputList[i] = max;

cout << inputList[i] << " ";

11 Return product of odd elements where row index = column index (i == j).
Fix: Correct the loop start from i = 0, initialize product to 1, and handle odd check properly.

int productMatrix(int rows, int columns, int *matrix)

int result = 1;

bool foundOdd = false;

for (int i = 0; i < rows && i < columns; i++)

int value = *((matrix + i * columns) + i);

if (value % 2 != 0)

result *= value;

foundOdd = true;

return foundOdd ? result : 0;

12 Complete exponentiation to support negative exponents using the


given positiveExponent() function

float allExponent(int baseValue, int exponentValue)

float res = 1;
if (exponentValue >= 0)

res = (float)positiveExponent(baseValue, exponentValue);

else

res = 1.0 / positiveExponent(baseValue, -exponentValue);

return res;

13 eplace every element in an array with the maximum element.

void maxReplace(int size, int* inputList)

if (size > 0)

int max = inputList[0];

for (int i = 1; i < size; i++)

if (inputList[i] > max)

max = inputList[i];

for (int i = 0; i < size; i++)

inputList[i] = max;

cout << inputList[i] << " ";


}

14 The function/method productMatrix accepts three arguments - rows, an integer


representing the rows of the matrix; columns, an integer representing the columns of the
matrix and matrix, a two-dimensional array of integers, respectively. The function/method
productMatrix return an integer representing the product of the odd elements whose i and j
index are the same. Otherwise, it returns 0.

int productMatrix(int rows, int columns, int **matrix)

int result = 1;

bool found = false;

for(int i = 0; i < rows && i < columns; i++)

if(matrix[i][i] % 2 != 0)

result *= matrix[i][i];

found = true;

return found ? result : 0;

15 The function/method calculateMatrixSum returns an integer representing the sum of odd


elements of the given matrix whose i and j index are the same. The function/method
calculateMatrixSum accepts three arguments - rows, an integer representing the number of
rows of the given matrix, columns, an integer representing the number of columns of the
given matrix and matrix, representing a two-dimensional array of integers. The
function/method calculateMatrixSum compiles successfully but fails to return the desired
result for some test cases due to logical errors. Your task is to fix the code so that it passes all
the test cases

int calculateMatrixSum(int rows, int columns, int **matrix)

int sum = 0;

if ((rows > 0) && (columns > 0))

for (int i = 0; i < rows && i < columns; i++)

if (matrix[i][i] % 2 != 0)

sum += matrix[i][i];

return sum;

16 The function/method sortArray modify the input list by sorting its elements in descending
order. The function/method sortArray accepts two arguments - len, representing the length
of the list and arr, a list of integers representing the input list, respectively. The
function/method sortArray compiles successfully but fails to get the desired result for some
test cases due to logical errors. Your task is to fix the code so that it passes all the test cases

void sortArray(int len, int* arr) {

int i, j, max, location, temp;

for (i = 0; i < len - 1; i++) {

location = i;

for (j = i + 1; j < len; j++) {

if (arr[j] > arr[location]) {

location = j;

}
}

// Swap arr[i] and arr[location]

temp = arr[i];

arr[i] = arr[location];

arr[location] = temp;

17 .allExponent Function (Handle Positive & Negative Exponents)

You are expected to use the already defined function positiveExponent(int baseValue, int
exponentValue) for positive powers and extend the logic to support negative exponents

float allExponent(int baseValue, int exponentValue)

float res = 1;

if (exponentValue >= 0)

res = (float)positiveExponent(baseValue, exponentValue);

else

res = 1.0 / (float)positiveExponent(baseValue, -exponentValue);

return res;

You might also like