HCL Coding Questions and Answers
HCL Practice Coding Questions
As coding turns out to be the most crucial section of HCL recruitment examination, we believe that having a mindset for HCL Coding Questions & Answers is important. As the number of questions is only 2 but the time allotted is only 20 mins so it becomes very hard to pass all the test cases. Hence, this makes it obvious to understand the pattern of questions asked in HCL Placement Paper. This page will help you to know the solution of some coding questions.
About HCL Exam Pattern 2023
Topics | Number of Questions | Time | Difficulty |
---|---|---|---|
Numerical Ability | 15 | 15 mins | Medium |
Verbal Ability | 15 | 15 mins | Medium |
Reasoning Ability | 15 | 15 mins | Medium |
Computer Programming | 30 | 30 mins | High |
Coding | 2 | 20 mins | High |
HCL Coding Questions : Detailed Analysis
Find the complete analysis for HCL Coding Section below in the tabular format for better understanding.
HCL Section | No. of questions | Total Time |
---|---|---|
MCQ | 30 | 30 mins |
Coding Challenge | 2 | 20 mins |
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Some facts about Eligibility Criteria:-
HCL | Requirements |
---|---|
Qualification | B.Tech |
Eligible stream | (CSE,IT,ECE,EN,EI, ME) |
Percentage Criteria | 75% throughout academics (10th, 12th / Diploma & UG) |
Backlog | No Active Backlog |
Service Agreement | Probation period for selected SE/GET will be of 12 Months. |
Practice Coding Question for HCL
Question 1 :
PrepInsta Name : Move Hash to Front
Problem Statement : You have write a function that accepts, a string which length is “len”, the string has some “#”, in it you have to move all the hashes to the front of the string and return the whole string back and
print it.
char* moveHash(char str[],int n);
Example :
Sample Test Case
- Input:
Move#Hash#to#Front - Output:
###MoveHashtoFront
#include<stdio.h> #include<string.h> char *moveHash(char str[],int n) { char str1[100],str2[100]; int i,j=0,k=0; for(i=0;str[i];i++) { if(str[i]=='#') str1[j++]=str[i]; else str2[k++]=str[i]; } strcat(str1,str2); printf("%s",str1); } int main() { char a[100], len; scanf("%[^\n]s",a); len = strlen(a); moveHash(a, len); return 0; }
#include <bits/stdc++.h> using namespace std; string moveHash(string s) { string ans=""; for(auto i:s) if(i=='#') ans='#'+ans; else ans+=i; return ans; } int main() { string s; getline(cin,s); cout<< moveHash(s); }
import java.util.*; public class Main { public static void moveHash(String str ,int n) { String str1= new String(""); String str2= new String(""); int i=0; for(i=0;i < n;i++) { if(str.charAt(i)=='#') str1=str1 + str.charAt(i); else str2 = str2 + str.charAt(i); } String result = str1.concat(str2); System.out.println(result); } public static void main(String args[]) { Scanner sc = new Scanner(System.in); String a = sc.nextLine(); int len= a.length(); moveHash(a, len); } }
def moveHash(s): x=s.count("#") s=s.replace("#","") return ("#"*x+s) s=input() print(moveHash(s))
Question 2 :
PrepInsta Name : Borrow Number
Problem statement : You have two numbers number1 and number2, your job is to check the number of borrow operations needed for subtraction of number1 from number2. If the subtraction is not possible
then return the string not possible.
Example :
- 754
658 - Answer :
2
654
666 - Answer:
Not possible
#include <stdio.h> #include <string.h> int main() { int number1,number2,count=0; scanf("%d%d",&number1,&number2); if(number1< number2) printf("Not possible"); else { int flag=0; int temp1,temp2; while(number1!=0 && number2!=0) { temp1=0; temp2=number2%10; if(flag==1) temp1=number1%10-1; else temp1=number1%10; if(temp1< temp2) { count+=1; flag=1; } else flag=0; number1=number1/10; number2=number2/10; } printf("%d",count); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s1,s2; int c=0,f=0; cin>>s1>>s2; if(stoi(s1)< stoi(s2)) {cout<<"Impossible";} reverse(s1.begin(),s1.end()); reverse(s2.begin(),s2.end()); for(int i=0;i< s1.length();i++) if(s1[i]< s2[i]) {f=1;c++;} else if(s1[i]==s2[i]) { if(f==1) {c++;} f=0; } else f=0; cout<< c; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int number1=sc.nextInt(); int number2=sc.nextInt(); int count=0; if(number1< number2) { System.out.println("Not possible"); } else { boolean flag=false; while(number1!=0 && number2!=0) { int temp1=0; int temp2=number2%10; if(flag) temp1=number1%10-1; else temp1=number1%10; if(temp1< temp2) { count++; flag=true; } else flag=false; number1=number1/10; number2=number2/10; } System.out.println(count); } } }
number1=int(input()) number2=int(input()) count=0 if(number1< number2): print("Not possible") else: flag=0 while(number1!=0 and number2!=0): temp1=0 temp2=number2%10 if(flag): temp1=number1%10-1 else: temp1=number1%10 if(temp1< temp2): count+=1 flag=1 else: flag=0 number1=number1//10 number2=number2//10 print(count)
Question 3 :
PrepInsta Name : Capitalize/Decapitalize
Problem statement : You’re given a function that accepts the following, a string1, its length and a character c. Your job is to replace all the occurrences of character c in string1 and capitalize it or decapitalize it based on the character c.
Input :
hello world
l
Output :
heLLo worLd
Input :
prepinsta
p
Output :
PrePinsta
#include<stdio.h> #include<string.h> void changeCharacter (char str[], char chr, int len) { int i, j; for (i = 0; i < len; i++) if (chr == str[i]) { if (str[i] >= 64 && str[i] <= 92) str[i] += 32; else if (str[i] >= 90 && str[i] <= 122) str[i] -= 32; } printf ("%s", str); } int main () { char str[1000], chr; int len; scanf ("%[^\n]s", str); len = strlen (str); scanf (" %c", &chr); changeCharacter (str, chr, len); }
#include <bits/stdc++.h> using namespace std; int main() { string s; getline(cin,s); char k; cin>>k; for(auto i:s) if(i==k) { if(i>95)cout<<char(i-32); else cout<<char(i+32); } else cout<<i; }
import java.util.Scanner; public class Main { public static void change(String str, char c, int len) { char[] ch = str.toCharArray(); for(int i=0; i<ch.length; i++) { if( c == ch[i] ) { if(Character.isUpperCase(ch[i])) { ch[i]= Character.toLowerCase(ch[i]); } else if(Character.isLowerCase(ch[i])) { ch[i]= Character.toUpperCase(ch[i]); } } } System.out.print(new String(ch) ); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); char c = sc.next().charAt(0); int len = str.length(); change(str, c, len); } }
s=input() k=input() if(k.isupper()): s=s.replace(k,chr(ord(k)+32)) else: s=s.replace(k,chr(ord(k)-32)) print(s)
Question 4 :
Problem statement : You’re given a string where multiple characters are repeated consecutively. You’re supposed to reduce the size of this string using mathematical logic given as in the example below :
Input :
abbccccc
Output:
ab2c5
Input :
aabbbbeeeeffggg
Output:
a2b4e4f2g3
#include<stdio.h> int main() { char str[100]; scanf("%[^\n]s",str); int i, j, k=0, count = 0; char str1[100]; for(i=0; str[i]!='\0'; i++) { count = 0; for(j=0; j<=i; j++) { if(str[i]==str[j]) { count++; } } if(count==1) { str1[k] = str[i]; k++; } } for(i=0; i< k; i++) { count = 0; for(j=0; str[j]!='\0'; j++) { if(str1[i]==str[j]) { count++; } } if(count==1) { printf("%c",str1[i]); } else { printf("%c%d",str1[i],count); } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; getline(cin,s); map< char,int > m; for(auto i:s) m[i]++; for(auto i:m)cout<< i.first<< i.second; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); int i, j, k = 0, count = 0; String uni = new String(""); for(i=0; i<str.length(); i++) { count = 0; for(j=0; j<=i; j++) { if(str.charAt(i)==str.charAt(j)) { count++; } } if(count==1) { uni = uni + str.charAt(i); } } for(i=0; i <uni.length(); i++) { count = 0; for(j=0; j<str.length(); j++) { if(uni.charAt(i)==str.charAt(j)) { count++; } } if(count==1) { System.out.printf("%c",uni.charAt(i)); } else { System.out.printf("%c%d",uni.charAt(i),count); } } } }
s=input() i=1 c=1 while i<len(s): if s[i]==s[i-1]: c+=1 else: print(s[i-1],end="") print(c,end="") c=1 i+=1 print(s[i-1],end="") print(c)
Question 5 :
PrepInsta Name : Spiral Matrix
Problem statement : You will be given a 2d matrix. Write the code to traverse the matrix in a spiral format. Check the input and output for better understanding.
Example :
Input :
5 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
Output :
1 2 3 4 8 12 16 20 19 18 17 13 9 5 6 7 11 15 12 14 10
#include<stdio.h> #include<conio.h> int main() { int a[5][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20}}; int rs = 0, re = 5, cs = 0, ce = 4; int i, j, k=0; for(i=0;i<5;i++) { for(j=0;j<4;j++) { printf("%d ",a[i][j]); } printf("\n"); } while(rs< re && cs< ce) { for(i=cs;i< ce;i++) { printf("%d ",a[rs][i]); } rs++; for(i=rs;i< re;i++) { printf("%d ",a[i][ce-1]); } ce--; if(rs< re) { for(i=ce-1; i>=cs; --i) { printf("%d ", a[re - 1][i]); } re--; } if(cs< ce) { for(i=re-1; i>=rs; --i) { printf("%d ", a[i][cs]); } cs++; } } return 0; }
#include<bits/stdc++.h> using namespace std; void Spiral (vector < vector < int >>a) { if (a.size () == 0) return; int m = a.size (), n = a[0].size (); int i, k = 0, l = 0; while (k < m && l < n) { for (i = l; i < n; ++i) cout << a[k][i] << " "; k++; for (i = k; i < m; ++i) cout << a[i][n - 1] << " "; n--; if (k < m) { for (i = n - 1; i >= l; --i) cout << a[m - 1][i] << " "; m--; } if (l < n) { for (i = m - 1; i >= k; --i) cout << a[i][l] << " "; l++; } } } int main () { int r, c; cin >> r >> c; vector < vector < int >>mat (r, vector < int >(c)); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { cin >> mat[i][j]; } } Spiral (mat); }
import java.util.*; public class Main { public static List< Integer> solve(int [][]matrix,int row,int col) { List< Integer> res=new ArrayList< Integer>(); boolean[][] temp=new boolean[row][col]; int []arr1={0,1,0,-1}; int []arr2={1,0,-1,0}; int di=0,r=0,c=0; for(int i=0;i< row*col;i++) { res.add(matrix[r][c]); temp[r][c]=true; int count1=r+arr1[di]; int count2=c+arr2[di]; if(count1 >=0 && row >count1 && count2 >=0 && col >count2 && !temp[count1][count2]){ r=count1; c=count2; } else { di=(di+1)%4; r+=arr1[di]; c+=arr2[di]; } } return res; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int m=sc.nextInt(); int n=sc.nextInt(); int matrix[][]=new int[m][n]; for(int i=0;i < m;i++) { for(int j=0;j < n;j++) matrix[i][j]=sc.nextInt(); } System.out.println(solve(matrix,m,n)); } }
def spiralOrder(arr): ans=[] while arr: ans+=arr.pop(0) arr= (list(zip(*arr)))[::-1] return ans arr=[] n,m=map(int,input().split()) for i in range(n): arr.append(list(map(int,input().split()))) print(*spiralOrder(arr))
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