The CoCubes Coding Test is a crucial part of the hiring process for many top companies. It evaluates candidates on their problem-solving skills, logical reasoning, and programming abilities. This guide provides a comprehensive list of frequently asked CoCubes coding questions along with their solutions.
Two numbers are considered co-prime if their GCD (Greatest Common Divisor) is 1. Given an array, count the number of co-prime pairs present.
Input:
3
1 2 3
Output:
3
(Co-prime pairs: (1,2), (2,3), (1,3))
#include<stdio.h>
int gcd(int a, int b) {
while (a != 0) {
int temp = a;
a = b % a;
b = temp;
}
return b;
}
int count_coprime_pairs(int arr[], int n) {
int count = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (gcd(arr[i], arr[j]) == 1) {
count++;
}
}
}
return count;
}
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("%d", count_coprime_pairs(arr, n));
return 0;
}
Given an array and a number e1, find the Nth occurrence of e1 in the array. If it doesn’t exist, return -1.
Input:
7
1 4 6 7 6 3 6
6
3
Output:
6 (3rd occurrence of 6 is at index 6)
#include<stdio.h>
int main() {
int arr[100], i, n, num, occurrence, count = 0;
scanf("%d", &n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
scanf("%d %d", &num, &occurrence);
for(i = 0; i < n; i++) {
if(arr[i] == num) {
count++;
if(count == occurrence) {
printf("%d", i);
return 0;
}
}
}
printf("-1");
return 0;
}
Check whether a number exists in an array.
Input:
3
1 2 3
2
Output:
Found
#include<stdio.h>
int main() {
int n, i, x, arr[100];
scanf("%d", &n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
scanf("%d", &x);
for(i = 0; i < n; i++) {
if(arr[i] == x) {
printf("Found");
return 0;
}
}
printf("Missing");
return 0;
}
Find the second-largest number from the given array.
Input:
7
23 45 7 34 25 25 89
Output:
45
#include<stdio.h>
int main() {
int n, i, first, second;
scanf("%d", &n);
int arr[n];
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
first = second = -1;
for(i = 0; i < n; i++) {
if(arr[i] > first) {
second = first;
first = arr[i];
} else if(arr[i] > second && arr[i] != first) {
second = arr[i];
}
}
printf("%d", second);
return 0;
}
The CoCubes Coding Test is a great opportunity to showcase your problem-solving and coding skills. By practicing frequently asked questions like the ones above, you can significantly improve your chances of success. Stay consistent with your preparation, and you’ll be well on your way to clearing the test!