Sunteți pe pagina 1din 12

HCL Company Specific Placement Training

Question Bank

1. Count how many times the given digital clock shows identical digits
Given a generic digital clock, having h number of hours and m number of minutes, the
task is to find how many times the clock shows identical time. A specific time is said to be
identical if every digit in the hours and minutes is same i.e. the time is of
type D:D, D:DD, DD:D or DD:DD.

Note that the time is written on the digital clock without any leading zeros and the clock
shows time between 0 to h – 1 hours and 0 to m – 1 minutes. Few examples of identical times
are:
 1:1
 22:22
 3:33
 11:1

Examples:
Input: hours = 24, minutes = 60
Output: 19
The clock has 24 hours and 60 minutes.
So the identical times will be:
Single digit hours and single digit minutes -> 0:0, 1:1, 2:2, …., 9:9
Single digit hours and double digit minutes -> 1:11, 2:22, 3:33, 4:44 and 5:55
Double digit hours and single digit minutes -> 11:1 and 22:2
Double digit hours and double digit minutes -> 11:11, 22:22
Total = 10 + 5 + 2 + 2 = 19
Input: hours = 34, minutes = 50
Output: 20

// C++ implementation of the approach


#include <bits/stdc++.h>
using namespace std;
// Function to return the count of
// identical times the clock shows
int countIdentical(int hours, int minutes) {
// To store the count of identical times
// Initialized to 1 because of 0:0
int i, count = 1;
// For single digit hour
for (i = 1; i <= 9 && i < hours; i++) {
// Single digit minute
if (i < minutes)
count++;
// Double digit minutes
if ((i * 10 + i) < minutes)
count++;
}
// For double digit hours
for (i = 11; i <= 99 && i < hours; i = i + 11) {
// Single digit minute
if (i < minutes)
count++;
// Double digit minutes
if ((i % 10) < minutes)
count++;
}
// Return the required count
return count;
}
int main()
{
int hours = 24;
int minutes = 60;
cout << countIdentical(hours, minutes);
return 0;
}

2. Print the pattern of the following form containing the numbers from --
to ---.
4444444
4333334
4322234
4321234
4322234
4333334
4444444
Input Format
The input will contain a single integer .
Constraints
Output Format
Print the pattern mentioned in the problem statement.
Sample Input 0
2
Sample Output 0
222
212
222
Sample Input 1
5
Sample Output 1
555555555
544444445
543333345
543222345
543212345
543222345
543333345
544444445
555555555
Sample Input 2
7
Sample Output 2
7777777777777
7666666666667
7655555555567
7654444444567
7654333334567
7654322234567
7654321234567
7654322234567
7654333334567
7654444444567
7655555555567
7666666666667
7777777777777

// C implementation of the approach

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
{

int n;
scanf("%d", &n);
int len = n * 2 - 1;
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
int min = i < j ? i : j;
min = min < len - i ? min : len - i - 1;
min = min < len - j - 1 ? min : len - j - 1;
printf("%d ", n - min);
}
printf("\n");
}
// Complete the code to print the pattern.
return 0;
}

3. Count zeros in a sorted matrix


Given a N x N binary matrix A where each row and column of the matrix is sorted in ascending
order , Your task is to complete the function countZero which returns the count of number of 0s
present in it.

Note : Elements in matrix can be either 1 or 0

Input:
The first line of input will be the no of test cases then T test cases will follow . The second line of
each test case contains two space separated integers M,N denoting the size of the 2 d matrix .
Then in the next lines are the space separated values of the matrix A[ ] [ ] .

Output:
The output will be the number of zeroes present in the square matrix.

Constraints:
1<=T<=50
1<=M,N<=50
0<=A[][]<=1
Example:
Input
1
3
000001011
Output
6

// C++ implementation of the approach


#include <iostream>
using namespace std;
#define MAX 1000
int countZeroes(int A[MAX][MAX],int N);
int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int A[MAX][MAX];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>A[i][j];
cout<<countZeroes(A,n)<<endl;
}
return 0;
}
}
int countZeroes(int a[MAX][MAX],int N)
{
int count=0;
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
if(a[i][j]==0)
{
count++;
}
}
}
return count;
}

// Java implementation of the approach


import java.util.*;
class Count_0_In_Sorted_Matrix
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t > 0)
{
int n = sc.nextInt();
int arr[][] = new int[1000][1000];
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++ )
{
arr[i][j] = sc.nextInt();
}
}
System.out.println(new GfG().countZeros(arr, n));
t--;
}
}
}
}
class GfG
{
int countZeros(int A[][], int n)
{
// Your code here
int j,k;
int count=0;
for(j=0;j<n;j++){
for(k=0;k<n;k++){
if(A[j][k]==0){
count++;
}
}
}
return count;
}
}
}
}

4. Count the frequency of elements in the list contains only lower case
alphabets.
Note: use add() to append element in the list and contains() to check an element is present or not
in the list and collections.frequency() to find the frequency of the element in the list.
Input Format:
First line of testcase contains T, number of testcases. For each testcase, first line contains number
of queries Q. Each query may be any one of the two type:
1. "i" with "c" : insert the element "c" into the ArrayList
2. "f" with "c": find the frequency of "c" in the ArrayList.
Output Format:
For each testcase, output the frequency of elements. Print "Not Present" if element is not present
in the list else its frequency in new line for every query.
Your Task:
Your task is to complete the functions insert() and freq() which are used to insert and find
frequency of element respectively.
Constraints:
1 <= T <= 100
1 <= N <= 105
1 <= Q <= 102
Example:
Input:
2
6
igieieikisfe
4
icipipff
Output:
2
Not Present
Explanation:
Testcase 1: Inserting g, e, e, k, s into the list. Frequency of e is 2 in the list.
Testcase 2: Inserting c, p, p into the list. Frequency of f is 0 in the list.
// Java implementation of the approach
//Initial Template for Java
import java.util.*;
//Position this line where user code will be pasted.
// Driver class with driver code
class GFG
{
// Driver code
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();

while(t-- > 0)
{
// Declaring ArrayList
ArrayList<Character> clist = new ArrayList<Character>();

int q = sc.nextInt();

// Looping for queries


while(q-- > 0)
{
char ch = sc.next().charAt(0);
Geeks obj = new Geeks();

if(ch == 'i')
{
char c = sc.next().charAt(0);
obj.insert(clist, c);
}

if(ch == 'f')
{
char c = sc.next().charAt(0);
obj.freq(clist, c);
}
}
}
}
}
}
class Geeks
{
public static void insert(ArrayList<Character> clist, char c)
{

clist.add(c);
}

public static void freq(ArrayList<Character> clist, char c)


{

if(clist.contains(c))
System.out.println(Collections.frequency(clist,c));
else
System.out.println("Not Present");

}
}

6. Check if a given string is a rotation of a palindrome


Given a string, check if it is a rotation of a palindrome. For example your function should return
true for “aab” as it is a rotation of “aba”.
Examples:
Input: str = "aaaad"
Output: 1
// "aaaad" is a rotation of a palindrome "aadaa"

Input: str = "abcd"


Output: 0
// "abcd" is not a rotation of any palindrome.

// C++ implementation of the approach

#include<iostream>
#include<string>
using namespace std;

bool isPalindrome(string str)


{
// Start from leftmost and rightmost corners of str
int l = 0;
int h = str.length() - 1;

// Keep comparing characters while they are same


while (h > l)
if (str[l++] != str[h--])
return false;

return true;
}

bool isRotationOfPalindrome(string str)


{
// If string iteself is palindrome
if (isPalindrome(str))
return true;

int n = str.length();
for (int i = 0; i < n-1; i++)
{
string str1 = str.substr(i+1, n-i-1);
string str2 = str.substr(0, i+1);

if (isPalindrome(str1.append(str2)))
return true;
}
return false;
}

// Driver program to test above function


int main()
{
cout << isRotationOfPalindrome("aab") << endl;
cout << isRotationOfPalindrome("abcde") << endl;
cout << isRotationOfPalindrome("aaaad") << endl;
return 0;
}
Output:
1
0
1

7. Generic sort
You need to sort elements of an array where the array can be of following data-types:
 Integer
 String
 floating numbe
Your task is to complete the given two functions: sortArray() and printArray()
Input:
The input line contains T, denoting the number of testcases. Each testcase contains 2 lines. The
first line contains n(size of array) and q(type of array) separated by space. Below is the
description about q.
 q = 1, means elements of the array are of integer type
 q = 2, means elements of the array are of string type
 q = 3, means elements of array are of floating digit type
The second line contains n elements of the array separated by space.
Output:
For each testcase in new line, print the elements in sorted form of given type of array separated
by space.
Constraints:
1 <= T <= 50
1 <= n <= 100
1 <= q <= 3
Example:
Input:
3
33
34.23 -4.35 3.4
41
123 -2311 837 0
52
focus on challenges in implementing
Output:
-4.35 3.4 34.23
-2311 0 123 837
challenges focus implementing in on
Explanation:
Testcase 1: The array is of floating type. After sorting the elements of array are as such: -4.35
3.4 34.23

// C++ implementation of the approach


#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n, q, i;
cin>>n>>q;

int intArr[n];
string strArr[n];
float floatArr[n];

switch(q)
{
case 1:
for(i=0; i<n; i++)
{
cin>>intArr[i];
}
sortArray(intArr, n);
printArray(intArr, n);
break;

case 2:
for(i=0; i<n; i++)
{
cin>>strArr[i];
}
sortArray(strArr, n);
printArray(strArr, n);
break;

case 3:
for(i=0; i<n; i++)
{
cin>>floatArr[i];
}
sortArray(floatArr, n);
printArray(floatArr, n);
break;
}
}
return 0;
}

template <class T>


void sortArray(T a[], int n)
{
sort(a,a+n);
}
template <class T>
void printArray(T a[], int n)
{
int i;
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}

Output
For Input:
3
33
34.23 -4.35 3.4
41
123 -2311 837 0Â
52
focus on challenges in implementing
Your Output is:
-4.35 3.4 34.23
-2311 0 123 837

8. Searching a number
Given an array of N elements and a integer K. Your task is to return the position of first
occurence of K in the given array.
Note: Position of first element is considered as 1.
Input:
First line of input contains T denoting the number of testcases. For each testcase there will be
two space separated integer N and K denoting the size of array and the value of K
respectively. The next line contains the N space separated integers denoting the elements of
array.
Output:
For each test case, print the index of first occurrence of given number K. Print -1 if the
number is not found in array.
Constraints:
1 <= T <= 100
1 <= N <= 106
1 <= K <= 106
1 <= A[i] <= 106
Example:
Input :
2
5 16
9 7 2 16 4
7 98
1 22 57 47 34 18 66
Output :
4
-1

// C++ implementation of the approach


using namespace std;

int main() {
int t;
cin>>t;
while(t--)
{
int n,k;
cin>>n>>k;
int i;
int *a=new int[n];
for(i=0;i<n;i++)
cin>>a[i];
int flag=0;
int index=0;
for(i=0;i<n;i++)
{
if(a[i]==k)
{
flag=1;
index=i;
break;
}
}
if(flag==1)
cout<<index+1<<endl;
else
cout<<-1<<endl;
}
return 0;
}

S-ar putea să vă placă și