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 –
- C
- C++
- Java
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 Question | Suggested Avg. Time | Difficulty |
---|---|---|
Question 1 | 5-7 mins | Medium |
Question 2 | 15-18 mins | Medium |
Question 3 | 20-25 mins | High |
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?
#include <stdio.h> void findLeaders(int arr[], int size) { for (int i = 0; i < size; i++) { int j; for (j = i+1; j < size; j++) { if (arr[i] <= arr[j]) break; } if (j == size) printf("%d ",arr[i]); } } int main() { int len, i; printf("Enter the length of the array : "); scanf("%d",&len); int arr[len]; printf("Enter the elements of the array : "); for( i=0; i< len; i++) { scanf("%d",&arr[i]); } int n = sizeof(arr)/sizeof(arr[0]); findLeaders(arr, n); return 0; }
#include<iostream> using namespace std; void findLeaders(int arr[], int size) { for (int i = 0; i < size; i++) { int j; for (j = i+1; j < size; j++) { if (arr[i] <= arr[j]) break; } if (j == size) cout << arr[i] << " "; } } int main() { int len, i; cout<< "Enter the length of the array\n"; cin >> len; int arr[len]; cout<< "Enter the elements of the array\n"; for( i=0; i<len; i++) { cin>>arr[i]; } int n = sizeof(arr)/sizeof(arr[0]); findLeaders(arr, n); return 0; }
class Main { /*Java Function to print leaders in an array */ void printLeaders (int arr[], int size) { for (int i = 0; i < size; i++) { int j; for (j = i + 1; j < size; j++) { if (arr[i] <= arr[j]) break; } if (j == size) System.out.print(arr[i] + " "); } } /* Driver program to test above functions */ public static void main (String[]args) { Main lead = new Main (); int arr[] = new int[]{ 16, 17, 4, 3, 5, 2 }; int n = arr.length; lead.printLeaders (arr, n); } }
def solve(arr,n): count=1 for i in range(n-1): flag=1 for j in range(i+1,n): if(arr[i]<=arr[j]): flag=0 break if(flag):count+=1 return count n=int(input()) arr=list(map(int,input().split())) print(solve(arr,n))
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.
#include<stdio.h> int maxDiff (int arr[], int arr_size) { int max_diff = arr[1] - arr[0]; int i, j; for (i = 0; i < arr_size; i++) { for (j = i + 1; j < arr_size; j++) { if (arr[j] - arr[i] > max_diff) max_diff = arr[j] - arr[i]; } } return max_diff; } int main () { int len, i; printf( "Enter the length of the array\n"); scanf("%d",&len); int arr[len]; printf( "Enter the elements of the array\n"); for( i=0; i< len; i++) { scanf("%d",&arr[i]); } printf ("Maximum difference is %d" , maxDiff (arr, len)); return 0; }
#include <iostream> using namespace std; int maxDiff (int arr[], int arr_size) { int max_diff = arr[1] - arr[0]; int i, j; for (i = 0; i < arr_size; i++) { for (j = i + 1; j < arr_size; j++) { if (arr[j] - arr[i] > max_diff) max_diff = arr[j] - arr[i]; } } return max_diff; } int main() { int len, i; cout << "Enter the length of the array\n"; cin>>len; int arr[len]; cout<< "Enter the elements of the array\n"; for( i=0; i< len; i++) { cin>>arr[i]; } cout<< maxDiff (arr, len); return 0; }
import java.util.*; class Main { int maxDiff(int arr[], int arr_size) { int max_diff = arr[1] - arr[0]; int i, j; for (i = 0; i < arr_size; i++) { for (j = i + 1; j < arr_size; j++) { if (arr[j] - arr[i] > max_diff) max_diff = arr[j] - arr[i]; } } return max_diff; } public static void main(String[] args) { Main maxdif = new Main(); int arr[] = { 1, 2, 90, 10, 110 }; System.out.println("Maximum differnce is " + maxdif.maxDiff(arr, 5)); } }
def solve(arr,n): max_diff = arr[1] - arr[0] for i in range(n): for j in range(i+1,n): if (arr[j] - arr[i] > max_diff): max_diff = arr[j] - arr[i] return max_diff n=int(input()) arr=list(map(int,input().split())) print(solve(arr,n))
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
#include <stdio.h> int longest(char str[], int n) { int length = 0, i = n/2; if( n < 2 ) return 1; else { while( str[i]!='\0' ) { //When we find the character like prefix in suffix, //we will move the length and i to count the length of the similar prefix and suffix if (str[i] == str[length]) { ++length; ++i; } else //When prefix and suffix not equal { if(length == 0) ++i; else --length; } } return length; } } int main() { char str[] = {"aaaa"}; int n = sizeof(str)/sizeof(str[0]); int length = longest(str, n); printf("%d", length); return 0; }
#include <bits/stdc++.h> using namespace std; int lps (string); int main () { int T; cin >> T; getchar (); while (T--) { string s; cin >> s; printf ("%d \n ", lps (s)); } return 0; } int lps (string s) { int n = s.size (); int lps[n]; int i = 1, j = 0; lps[0] = 0; while (i < n) { if (s[i] == s[j]) { j++; lps[i] = j; i++; } else { if (j != 0) j = lps[j - 1]; else { lps[i] = 0; i++; } } } return lps[n - 1]; }
import java.util.*; import java.lang.*; import java.io.*; class Main { public static void main (String[]args) { Scanner s = new Scanner (System.in); int t = s.nextInt (); for (int i = 0; i < t; i++) { String s1 = s.next (); int j = 1, k = 0, l = s1.length (), max = 0, len = 0; int lps[] = new int[l]; while (j < l) { if (s1.charAt (j) == s1.charAt (len)) { len++; lps[j] = len; j++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[j] = 0; j++; } } } System.out.println (lps[l - 1]); } } }
def solve(s,n) : for i in range(n // 2, 0, -1) : start= s[0:i] end= s[n-i: n] if (start== end) : return i return 0 T=int(input()) for i in range(T): s=input() print(solve(s,len(s)))
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
#include <stdio.h> #include <stdlib.h> int closestNumber (int n, int m) { // find the quotient int q = n / m; // 1st possible closest number int n1 = m * q; // 2nd possible closest number int n2 = (n * m) > 0 ? (m * (q + 1)) : (m * (q - 1)); // if true, then n1 is the required closest number if (abs (n - n1) < abs (n - n2)) return n1; else // else n2 is the required closest number return n2; } int main () { int n, m; printf("Enter two numbers : "); scanf("%d",&n); scanf("%d",&m); printf("%d",closestNumber(n,m)); return 0; }
#include <bits/stdc++.h> using namespace std; int closestNumber (int n, int m) { // find the quotient int q = n / m; // 1st possible closest number int n1 = m * q; // 2nd possible closest number int n2 = (n * m) > 0 ? (m * (q + 1)) : (m * (q - 1)); // if true, then n1 is the required closest number if (abs (n - n1) < abs (n - n2)) return n1; // else n2 is the required closest number return n2; } int main () { int n, m; cout<< "Enter the two numbers\n"; cin>> n; cin>> m; cout << closestNumber (n, m) << endl; return 0; }
import java.util.*; class Main { // function to find the number closest to n // and divisible by m static int closestNumber (int n, int m) { // find the quotient int q = n / m; // 1st possible closest number int n1 = m * q; // 2nd possible closest number int n2 = (n * m) > 0 ? (m * (q + 1)) : (m * (q - 1)); // if true, then n1 is the required closest number if (Math.abs (n - n1) < Math.abs (n - n2)) return n1; // else n2 is the required closest number return n2; } // Driver program to test above public static void main (String args[]) { Scanner sc=new Scanner (System.in); int n=sc.nextInt(); int m=sc.nextInt(); System.out.println (closestNumber (n, m)); } }
def solve(s,n) : q = n//m n1 = m * q n2 = (m * (q + 1)) if (n * m) > 0 else(m * (q - 1)) if (abs (n - n1) < abs (n - n2)): return n1 else: return n2 n=int(input()) m=int(input()) print(solve(n,m))
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
#include <stdio.h> #include <string.h> int evaluateBoolExpr(char* s) { int n = strlen(s); for (int i = 0; i < n; i += 2) { // If operator next to current operand // is AND. if (i + 1 < n && i + 2 < n) { if (s[i+1] == 'A') { if (s[i+2] == '0'||s[i] == '0') s[i+2] = '0'; else s[i+2] = '1'; } // If operator next to current operand // is OR. else if ((i + 1) < n && s[i+1] == 'B') { if (s[i+2] == '1'||s[i] == '1') s[i+2] = '1'; else s[i+2] = '0'; } // If operator next to current operand // is XOR (Assuming a valid input) else { if (s[i+2] == s[i]) s[i+2] = '0'; else s[i+2] = '1'; } } } return s[n-1] - '0'; } int main() { char str[100]; scanf("%[^\n]s",str); int result = evaluateBoolExpr(str); printf("%d",result); }
// Java program to evaluate value of an expression. class Main { // Evaluates boolean expression // and returns the result static int evaluateBoolExpr (StringBuffer s) { int n = s.length (); // Traverse all operands by jumping // a character after every iteration. for (int i = 0; i < n; i += 2) { // If operator next to current operand // is AND. if (i + 1 < n && i + 2 < n) { if (s.charAt (i + 1) == 'A') { if (s.charAt (i + 2) == '0'||s.charAt (i) == 0) s.setCharAt (i + 2, '0'); else s.setCharAt (i + 2, '1'); } // If operator next to current operand // is OR. else if ((i + 1) < n && s.charAt (i + 1) == 'B') { if (s.charAt (i + 2) == '1'||s.charAt (i) == '1') s.setCharAt (i + 2, '1'); else s.setCharAt (i + 2, '0'); } // If operator next to current operand // is XOR (Assuming a valid input) else { if (s.charAt (i + 2) == s.charAt (i)) s.setCharAt (i + 2, '0'); else s.setCharAt (i + 2, '1'); } } } return s.charAt (n - 1) - '0'; } // Driver code public static void main (String[]args) { String s = "1C1B1B0A0"; StringBuffer sb = new StringBuffer (s); System.out.println (evaluateBoolExpr (sb)); } }
def solve(s): s=s.replace("A","&").replace("B","|").replace("C","^") return eval(s) s=input() print(solve(s))
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
import java.util.Scanner; public class Main { public static void main (String[]args) { try (Scanner sc = new Scanner (System.in); ) { StringBuilder sb = new StringBuilder (sc.nextLine ()); int count = 1; char current = sb.charAt (0), next; for (int i = 1; i < sb.length (); i++) { next = sb.charAt (i); if (next == current) { sb.deleteCharAt (i); i = i - 1; count++; } else { sb.insert (i, count); count = 1; current = next; i = i + 1; } } sb.append (count); System.out.println (sb); } } }
def solve(s): ans="" c=1 for i in range(len(s)-1): if(s[i]==s[i+1]): c+=1 else: if(c==1): ans+=s[i] else: ans+=s[i]+str(c) c=1 if(c==1): ans+=s[i+1] else: ans+=s[i+1]+str(c) return ans s=input() print(solve(s))
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
import java.util.Scanner; public class Main { public static void main (String[]args) { try (Scanner sc = new Scanner (System.in); ) { StringBuilder sb = new StringBuilder (sc.nextLine ()); StringBuilder s = new StringBuilder (); Boolean finish = true; while (finish) { if (isPalindrome (sb)) { System.out.println("NULL"); finish = false; } else { s.append (sb.charAt (0)); sb = new StringBuilder (sb.substring (1)); } } System.out.println (s.reverse ()); } } public static Boolean isPalindrome (StringBuilder sb) { int l = sb.length () / 2; int m = sb.length () - 1; for (int i = 0; i <= l; i++) { if (!(sb.charAt (i) == sb.charAt (m - i))) return false; } return true; } }
def ispalindrome(s): return s==s[::-1] def solve(s): if(ispalindrome(s)): return None for i in range(len(s)): x=s[:i][::-1] if(ispalindrome(s+x)): return x s=input() print(solve(s))
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
#include <stdio.h> #include <string.h> int solve(int n,int m) { if(n>m) return (n*m)-(n-m); else return (m%n)-(m+n); } int main() { int n,m; scanf("%d%d",&n,&m); int result = solve(n,m); printf("%d",result); }
#include <iostream> using namespace std; int solve(int n,int m) { if(n>m) return (n*m)-(n-m); else return (m%n)-(m+n); } int main() { int n,m; cin>>n>>m; int result = solve(n,m); cout<< result; return 0; }
import java.util.Scanner; import java.io.*; class Main { public static void main (String[] args) { Scanner prep = new Scanner( System.in ); int n,m; n= prep.nextInt(); m= prep.nextInt(); System.out.println(solve(n,m)); } public static int solve(int n, int m) { if(n>m) return (n*m)-(n-m); else return (m%n)-(m+n); } }
def solve(n,m): if(n>m): return (n*m)-(n-m) else: return (m%n)-(m+n) n=int(input()) m=int(input()) print(solve(n,m))
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
def solve(arr,n): count=0 for i in set(arr): if(arr.count(i)%2==1): count+=i return count n=int(input()) arr=list(map(int,input().split())) print(solve(arr,n))
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
#include <stdio.h> #include<string.h> int solve(char str[]) { int count= 0; for (int i = 0; str[i]!='\0'; i++) { if ((str[i] >= 'A' && str[i] <= 'Z')|| (str[i] >= 'a' && str[i] <= 'z') ) count++; if(str[i] >= '0' && str[i] <= '9') count++; } return count; } int main() { char str[]= "Hello World!123"; printf("%d",solve(str)); return 0; }
#include <iostream> using namespace std; int solve(string str) { int count= 0; for (int i = 0; i < str.length(); i++) { if ((str[i] >= 'A' && str[i] <= 'Z')|| (str[i] >= 'a' && str[i] <= 'z') ) count++; if(str[i] >= '0' && str[i] <= '9') count++; } return count; } int main() { string str = "Hello World!123"; cout<< solve(str); return 0; }
import java.util.Scanner; import java.io.*; class Main { public static void main (String[] args) { System.out.println(solve("Hello World!123")); } public static int solve(String str) { int count= 0; char[] str1 = str.toCharArray(); for (int i = 0;i< str1.length; i++) { if ((str1[i] >= 'A' && str1[i] <= 'Z')|| (str1[i] >= 'a' && str1[i] <= 'z') ) count++; if(str1[i] >= '0' && str1[i] <= '9') count++; } return count; } }
def solve(str): count=0 for i in str: if(i.isalpha() or i.isnumeric()): count+=1 return count n=input() print(solve(n))
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
import java.util.*; class Main{ public static void main(String[] Args) { Scanner s = new Scanner(System.in); String str = s.nextLine(); System.out.println(solve(str)); } static char solve(String str ) { String v="aeiou"; int[] count = {0,0,0,0,0}; for(int i = 0; i<v.length(); i++) { if(v.charAt(i) == 'a') count[0]++; else if(v.charAt(i) == 'e') count[1]++; else if(v.charAt(i) == 'i') count[2]++; else if(v.charAt(i) == 'o') count[3]++; else if(v.charAt(i) == 'u') count[4]++; } int largest = 0; for ( int i = 1; i < count.length; i++ ) { if ( count[i] > count[largest] ) largest = i; } return v.charAt(largest); } }
def solve(str): v="aeiou" count=[0]*5 for i in str: if(i=='a'):count[0]+=1 elif(i=='e'):count[1]+=1 elif(i=='i'):count[2]+=1 elif(i=='o'):count[3]+=1 elif(i=='u'):count[4]+=1 return v[count.index(max(count))] n=input() print(solve(n))
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
def solve(start,end,k): count=0 for i in range(start,end+1): if(i%10==k): count+=1 return count a=int(input()) b=int(input()) k=int(input()) print(solve(a,b,k))
import java.util.*; class Main{ public static void main(String[] args) { Scanner s = new Scanner(System.in); int a = s.nextInt(); int b = s.nextInt(); int c = s.nextInt(); System.out.println(solve(a,b,c)); } public static int solve(int start , int end, int k) { int count = 0; for(int i = start;i<= end; i++ ) { if(i%10 == k) count+=1; } return count; } }
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
def solve(arr,n,k): count=0 for i in arr: x=0 while(i>0): i//=10 x+=1 if(x==k): count+=1 return count n=int(input()) arr=list(map(int,input().split())) k=int(input()) print(solve(arr,n,k))
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
def solve(n): s=0 for i in range(1,n+1): j=n//i s+=(i*j) return s n=int(input()) print(solve(n))
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
30+ Companies are Hiring
Get Hiring Updates right in your inbox from PrepInsta
Is python allowed in CoCubes?
l=[]
n=int(input())
l=list(map(int,input().split(‘ ‘)))
k=int(input())
c=0
for i in l:
i=str(i)
if(k==len(i)):
c+=1
print(c)