CoCubes Coding Questions and Programming Paper

CoCubes Coding Questions

Find all the latest Cocubes Coding Questions and Cocubes Programming Questions. With these programs you will get a lot of coding languages on how to solve different Cocubes Java Questions with Answers, Cocubes Programming Questions in Java.

You can use the following Languages –

  1. C
  2. C++
  3. Java
CoCubes Coding Questions

CoCubes Programming Round Details

There will be 3 questions in CoCubes Programming Section, for which the total time alloted is 45 mins. The difficulty level of questions is progressive, i.e; the first question will be very basic, the second question will be a bit advance and the third question will be difficult and tricky. So we will suggest that you should practice a good number of questions so that you can easily solve the first 2 questions with 10-15 mins, so that you have enough time for solving the last question

Note: You need not to write complete question but you need to fill the desired function that given. Inputs were already read and passed to the function

CoCubes Coding QuestionSuggested Avg. TimeDifficulty
Question 15-7 minsMedium
Question 215-18 minsMedium
Question 320-25 minsHigh

Question 1

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?

Question 2

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.

 

Question 3

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
2

Question 4

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  

Question 5

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

Question 6

Make a function which 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

Question 7

Write a function which 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

Question 8

Write a function which returns an integer based on some conditions. You were given with two integers as input say n and m

  • if n>m return (n*m)-(n-m)
  • if n<=m return (m%n)-(m+n)

Example:

Sample input:
n=10
m=18

Sample output:
-20

Explanation:
m%n=18%10=8 
m+n=28 
answer= 8-28=-20

Question 9

Write a function which returns the sum of elements whose frequency in the array is odd. Means find sum of elements whose Number of occurrences is odd  

Example:

Input:
15
arr=[1,1,2,2,2,3,4,4,5,5,5,5,6,7,7]

Output:
11

Explanation:
count of each element is as follows-
1–>2, 2–>3, 3–>1, 4–>2, 5–>4, 6–>1, 7–>2
Odd number of time occured elements are, 2,3,6 and its sum if 11

Question 10

Write a function to return the count of  alphanumeric characters in a given string.(Count number of alphabets and numerics in a string)

Example :

Input:
Hello World!123

Output:
13

Question 11

find the sum of the digits in a number until its sum is equal to single digit. Consider the below example for better understand

#testcase1:

Input:
123

Output:
6

Explanation:
1+2+3=6

#testcase2:

Input:
8448440710

Output:
4

Explanation:
=8+4+4+8+4+4+0+7+1+0
=40 =4+0
4

Question 12

In English alphabet a,e,i,o,u are called as vowels. Write a function to return the most frequent vowel used in the given input string.

Note: All characters are lower case English alphabets

Example:

Input:
abeabutiedcia

Output:
a

Explanation:
a occured 3 times

Question 13

Return the count of numbers whose unit digit is end with the integer ‘k’ which is one of the input given to the function.

You were given with three inputs starting range , ending  range and the integer k.

Example:

Input: 
start=10
end=54
k=2

Output:
5

Explanation:
numbers with unit digit as 2 in the given range is 12,22,32,42,52

Question 14

Find the count of numbers in the array which has K digits in it.

Note: size of array and the value of K is always greater than zero.

Example:

Input:
arr=[10,22,3,4,1,6,7,8,5,33,4,99,100]
k=2

Output:
4

Explanation:
2 digit numbers are 10,22,33,99

Question 15

Find SoP (sum of product). You were given with a integer n and you have to find sum of all products of i*j where 

  • i ranges from 1 to n, including both ranges
  • j is n/i  for every i

Find i*j for every iteration and sum them up to the final output

Example:

Input:
n=4

Output:
15

Explanation: 
i=1 , j=4/1=4 , i*j=4
i=2 , j=4/2=2, i*j=4
i=3,  j=4/3=1,  i*j=3
i=4,  j=4/4=1,  i*j=4

Output:
4+4+3+4=15

Check out this video below on Cocubes Preparation Tips by PrepInsta, know more about cut off marks pattern and companies using Cocubes and other Tips and Tricks

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

2 comments on “CoCubes Coding Questions and Programming Paper”