Cocubes Coding Questions & Answers | Prep for Cocubes Test

Cocubes Coding Questions & Answers | Prep for Cocubes Test

Cocubes Coding Questions & Answers | Prep for Cocubes Test

Introduction

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.

What You’ll Find in This Guide:

  • Commonly asked CoCubes coding questions with solutions
  • Sample input and output for each question
  • Programming logic and explanations
  • Pro tips to ace the test

1) Count the Number of Co-Prime Pairs in an Array

Problem Statement:

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.

Sample Input & Output:

Input:
3
1 2 3

Output:
3
(Co-prime pairs: (1,2), (2,3), (1,3))

C Solution:

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

2) Find the Nth Occurrence of a Number in an Array

Problem Statement:

Given an array and a number e1, find the Nth occurrence of e1 in the array. If it doesn’t exist, return -1.

Sample Input & Output:

Input:
7
1 4 6 7 6 3 6
6
3

Output:
6 (3rd occurrence of 6 is at index 6)

C Solution:

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

3) Search for an Element in an Array

Problem Statement:

Check whether a number exists in an array.

Sample Input & Output:

Input:
3
1 2 3
2

Output:
Found

C Solution:

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

4) Find the Second Largest Number in an Array

Problem Statement:

Find the second-largest number from the given array.

Sample Input & Output:

Input:
7
23 45 7 34 25 25 89

Output:
45

C Solution:

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

Tips to Ace the CoCubes Coding Test

  1. Understand the Problem Statement – Read the questions carefully before attempting.
  2. Optimize Your Code – Use efficient algorithms to minimize time complexity.
  3. Practice Regularly – Solve past papers and mock tests.
  4. Use Debugging Techniques – Check for edge cases and incorrect logic.
  5. Master Core Concepts – Revise arrays, strings, searching, sorting, and recursion.

Conclusion

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!

Cocubes Coding Questions & Answers | Prep for Cocubes Test