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.

HCL Coding Questions and Answers 2024

About HCL Exam Pattern 2025

TopicsNumber of QuestionsTimeDifficulty
Numerical Ability1515 minsMedium
Verbal Ability1515 minsMedium
Reasoning Ability1515 minsMedium
Computer Programming3030 minsHigh
Coding220 minsHigh

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:-

The eligibility criteria for HCL is given below in the tabulated format.
HCLRequirements
QualificationB.Tech
Eligible stream(CSE,IT,ECE,EN,EI, ME)
Percentage Criteria75% throughout academics (10th, 12th / Diploma & UG)
BacklogNo Active Backlog
Service AgreementProbation 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
Run
#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;
}

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

Run

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

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

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

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

Run

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

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

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

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