HCLIT Codes Zone

HCL Previous Coding Questions

๐Ÿ”ฅ HCL Previous Coding Questions with Java & Python Solutions

Welcome to this specially curated post featuring HCL Previous Coding Questions asked in real placement drives and technical assessments.
Each problem includes:

โœ… A clear and structured problem statement
โœ… Java and Python solutions
โœ… Example input and output
โœ… Concise explanation to understand the logic

Whether you’re preparing for HCL interviews or just want to sharpen your coding skills โ€” this guide will help you practice and master the most frequently asked problems.
Letโ€™s dive in! ๐Ÿš€


โœ… Problem 1: Sum of Digits in a String

๐Ÿ“ Problem Statement

You are given a string containing a mix of alphabets, digits, and possibly special characters. Your task is to extract all digits and calculate their total sum. This question tests your ability to handle string traversal and conditional logic efficiently.

๐Ÿ”ข Input

  • A string with a mix of characters (letters, digits, symbols).
  • Length โ‰ค 100 characters.

๐Ÿงพ Output

  • An integer representing the sum of all numeric digits in the input.

โœ… Example

Input:
abc123def45
Output:
15

๐Ÿ”ฃCode

๐Ÿ” Explanation

The program loops through each character in the string, checks if itโ€™s a digit, and adds its value to the total sum.

โœ… Problem 2: Reverse Each Word in a Sentence

๐Ÿ“ Problem Statement

Write a program to reverse every word in a given sentence, keeping the word order the same. This problem tests string splitting and word-level manipulation.

๐Ÿ”ข Input

  • A single line sentence with multiple words.

๐Ÿงพ Output

  • The sentence with each word reversed individually.

โœ… Example

Input:
hello world
Output:
olleh dlrow

๐Ÿ”ฃCode

๐Ÿ” Explanation

Split the sentence into words, reverse each word, and join them back with spaces.

โœ… Problem 3: Count Vowels in a String

๐Ÿ“ Problem Statement

Write a program that counts the number of vowels (a, e, i, o, u) in a string, ignoring case. This checks basic character handling.

๐Ÿ”ข Input

  • A lowercase or mixed-case string.

๐Ÿงพ Output

  • Total number of vowels found.

โœ… Example

Input:
Education
Output:
5

๐Ÿ”ฃCode

๐Ÿ” Explanation

Each character is checked against a list of vowels. If it matches, the count is increased.

โœ… Problem 4: Check Palindrome String

๐Ÿ“ Problem Statement

Determine whether the given string is a palindrome (reads the same forward and backward). Ignore case and assume no spaces or symbols.

๐Ÿ”ข Input

  • A single word or string.

๐Ÿงพ Output

  • True if it is a palindrome, otherwise False.

โœ… Example

Input:
madam
Output:
True

๐Ÿ”ฃCode

๐Ÿ” Explanation

The string is reversed and compared to the original (ignoring case).

โœ… Problem 5: Find Maximum Number in List

๐Ÿ“ Problem Statement

Given a list of integers, find the largest number using a simple loop. This is useful to test basic iteration and condition logic.

๐Ÿ”ข Input

  • A list of integers separated by spaces.

๐Ÿงพ Output

  • The maximum number in the list.

โœ… Example

Input:
10 20 45 6 78
Output:
78

๐Ÿ”ฃCode

๐Ÿ” Explanation

Compare each number in the list and store the highest seen so far in a variable.

โœ… Problem 6: Count Words in a Sentence

๐Ÿ“ Problem Statement

Write a program to count the number of words in a sentence. Words are separated by spaces. This tests basic string manipulation and understanding of delimiters.

๐Ÿ”ข Input

  • A sentence containing words separated by spaces.

๐Ÿงพ Output

  • An integer representing the word count.

โœ… Example

Input:
HCL is hiring software engineers
Output:
5

๐Ÿ”ฃCode

๐Ÿ” Explanation

Split the sentence into words using spaces and return the length of the resulting array/list.

โœ… Problem 7: Print Fibonacci Series up to N Terms

๐Ÿ“ Problem Statement

Write a program to print the first N terms of the Fibonacci sequence. This helps evaluate your loop control and series logic.

๐Ÿ”ข Input

  • An integer N representing the number of terms.

๐Ÿงพ Output

  • N Fibonacci numbers separated by spaces.

โœ… Example

Input:
5
Output:
0 1 1 2 3

๐Ÿ”ฃCode

๐Ÿ” Explanation

Fibonacci starts with 0 and 1, and each number is the sum of the previous two. Loop N times to print the sequence.

โœ… Problem 8: Find Factorial of a Number

๐Ÿ“ Problem Statement

Write a program to find the factorial of a non-negative integer N using iteration. This assesses your understanding of loops and multiplication.

๐Ÿ”ข Input

  • A non-negative integer N.

๐Ÿงพ Output

  • Factorial of the number.

โœ… Example

Input:
5
Output:
120

๐Ÿ”ฃCode

๐Ÿ” Explanation

Multiply all numbers from 2 to N to get the factorial. Loop handles the multiplication iteratively.

โœ… Problem 9: Print Even Numbers from 1 to N

๐Ÿ“ Problem Statement

Write a program that prints all even numbers between 1 and N. This helps check your grasp of modulus operations and loops.

๐Ÿ”ข Input

  • An integer N.

๐Ÿงพ Output

  • All even numbers from 1 to N.

โœ… Example

Input:
10
Output:
2 4 6 8 10

๐Ÿ”ฃCode

๐Ÿ” Explanation

Start from 2 and increment by 2 to directly get even numbers, avoiding unnecessary checks.

โœ… Problem 10: Reverse a Number

๐Ÿ“ Problem Statement

Given a number, write a program to reverse its digits. This problem tests your ability to use modulus and integer division.

๐Ÿ”ข Input

  • A positive integer.

๐Ÿงพ Output

  • Reversed number.

โœ… Example

Input:
1234
Output:
4321

๐Ÿ”ฃCode

๐Ÿ” Explanation

Use modulus to get last digit and build the reverse number by shifting existing digits left (multiply by 10).

โœ… Problem 11: Find the Maximum of Three Numbers

๐Ÿ“ Problem Statement

Write a program to find the largest of three numbers. This tests your understanding of conditional statements and comparison operators.

๐Ÿ”ข Input

  • Three integers a, b, and c

๐Ÿงพ Output

  • The largest of the three numbers

โœ… Example

Input:
5 12 9
Output:
12

๐Ÿ”ฃCode

๐Ÿ” Explanation

Use built-in methods or nested comparisons to find the greatest among three values.

โœ… Problem 12: Check Whether a Number is Prime

๐Ÿ“ Problem Statement

Write a program to check whether a number is a prime or not. This helps evaluate loop, condition checking, and divisor logic.

๐Ÿ”ข Input

  • An integer n

๐Ÿงพ Output

  • “Prime” if the number is prime, else “Not Prime”

โœ… Example

Input:
7
Output:
Prime

๐Ÿ”ฃCode

๐Ÿ” Explanation

Check divisibility from 2 to โˆšn. If divisible by any, itโ€™s not prime.

โœ… Problem 13: Sum of Digits of a Number

๐Ÿ“ Problem Statement

Given an integer, write a program to calculate the sum of its digits. This is a basic math problem involving modulus and loops.

๐Ÿ”ข Input

  • An integer n

๐Ÿงพ Output

  • Sum of digits

โœ… Example

Input:
123
Output:
6

๐Ÿ”ฃCode

๐Ÿ” Explanation

Use modulus to extract digits and add them in a loop.

โœ… Problem 14: Find GCD of Two Numbers

๐Ÿ“ Problem Statement

Write a program to compute the greatest common divisor (GCD) of two numbers using the Euclidean algorithm.

๐Ÿ”ข Input

  • Two integers a and b

๐Ÿงพ Output

  • The GCD of the two numbers

โœ… Example

Input:
24 36
Output:
12

๐Ÿ”ฃCode

๐Ÿ” Explanation

Use the Euclidean algorithm: GCD(a, b) = GCD(b, a % b) until b becomes 0.

โœ… Problem 15: Find the Second Largest Number

๐Ÿ“ Problem Statement

Given an array of integers, write a program to find the second largest distinct number. Make sure to ignore duplicate highest values.

๐Ÿ”ข Input

  • An array of integers

๐Ÿงพ Output

  • An integer representing the second highest unique number.

โœ… Example

Input:
[10, 20, 4, 45, 99, 99]
Output:
45

๐Ÿ”ฃCode

๐Ÿ” Explanation

To avoid counting duplicates, we convert the list to a set. Then we sort the set and return the second last item. Sorting is key to ensure proper comparison.

โœ… Problem 16: Toggle Case of String

๐Ÿ“ Problem Statement

Write a program to toggle each characterโ€™s case from a string. Uppercase becomes lowercase, and lowercase becomes uppercase.

๐Ÿ”ข Input

  • A string

๐Ÿงพ Output

  • A toggled version of input string

โœ… Example

Input:
“HeLLo”
Output:
“hEllO”

๐Ÿ”ฃCode

๐Ÿ” Explanation

Use built-in swapcase() in Python. In Java, loop through each character and flip the case using Character.isUpperCase() and conversion methods.

โœ… Problem 17: Sum of All Even Numbers from 1 to N

๐Ÿ“ Problem Statement

Write a program that takes an integer N and returns the sum of all even numbers from 1 to N. This helps test for loops and conditional logic.

๐Ÿ”ข Input

  • An integer(N)

๐Ÿงพ Output

  • Sum of even numbers from 1 to N.

โœ… Example

Input:
10
Output:
30 (2+4+6+8+10)

๐Ÿ”ฃCode

๐Ÿ” Explanation

Use a loop that starts from 2 and increments by 2. This avoids checking each number with if condition and is more efficient.

โœ… Problem 18: Remove Duplicates from List

๐Ÿ“ Problem Statement

Remove all duplicate elements from an input list while preserving the order of the first occurrence.

๐Ÿ”ข Input

  • A list of Integers

๐Ÿงพ Output

  • A list with unique elements in the given list

โœ… Example

Input:
[1, 2, 2, 3, 4, 3, 5]
Output:
[1, 2, 3, 4, 5]

๐Ÿ”ฃCode

๐Ÿ” Explanation

Use a set to track seen elements and a list to maintain order. This ensures duplicates are removed but original sequence is preserved.

โœ… Problem 19: Print Star Pyramid Pattern

๐Ÿ“ Problem Statement

Write a program that prints a star pyramid with N rows. This improves nested loop and pattern printing skills.

๐Ÿ”ข Input

  • An integer N (number of rows)

๐Ÿงพ Output

  • Pyramid pattern

โœ… Example

Input:
3
Output:

๐Ÿ”ฃCode

๐Ÿ” Explanation

Each row has spaces followed by stars based on the current row index.

โœ… Problem 20: Replace All Spaces with Underscores

๐Ÿ“ Problem Statement

Given a string, replace every space with an underscore _. This problem helps assess basic string manipulation.

๐Ÿ”ข Input

  • A sentence like “HCL placement question”

๐Ÿงพ Output

  • a sentence “HCL_placement_question”, by replacing spaces with underscore

โœ… Example

Input:
“HCL placement question”
Output:
“HCL_placement_question”

๐Ÿ”ฃCode

๐Ÿ” Explanation

String replace is a direct and efficient way to modify specific characters. Both languages provide this as a built-in function for convenience.

๐Ÿšจ HCL Overview!

Curious about how HCL’s selection process works, the rounds involved, and other key details? Donโ€™t miss this complete breakdown:
๐ŸŒ https://nextgenkodinghub.in/hcl-overview/

Wishing you the best of luck! ๐Ÿ’ผโœจ


Thanks for visiting! Explore the categories below for more exciting and useful content.


Leave a Reply

Your email address will not be published. Required fields are marked *