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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Main { public static void main(String[] args) { String input = "abc123def45"; int sum = 0; for (int i = 0; i < input.length(); i++) { char ch = input.charAt(i); if (Character.isDigit(ch)) { sum += Character.getNumericValue(ch); } } System.out.println(sum); } } |
1 2 3 4 5 6 7 8 9 10 |
def sum_of_digits(s): total = 0 for ch in s: if ch.isdigit(): total += int(ch) return total # Example input_str = "abc123def45" print(sum_of_digits(input_str)) |
π 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
1 2 3 4 5 6 7 8 9 10 11 |
public class Main { public static void main(String[] args) { String sentence = "hello world"; String[] words = sentence.split(" "); StringBuilder result = new StringBuilder(); for (String word : words) { result.append(new StringBuilder(word).reverse().toString()).append(" "); } System.out.println(result.toString().trim()); } } |
1 2 3 4 5 6 |
def reverse_words(sentence): words = sentence.split() result = [word[::-1] for word in words] return ' '.join(result) print(reverse_words("hello world")) |
π 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
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Main { public static void main(String[] args) { String input = "Education"; int count = 0; for (char ch : input.toCharArray()) { if ("aeiouAEIOU".indexOf(ch) != -1) { count++; } } System.out.println(count); } } |
1 2 3 4 5 6 7 8 9 |
def count_vowels(s): vowels = 'aeiouAEIOU' count = 0 for ch in s: if ch in vowels: count += 1 return count print(count_vowels("Education")) |
π 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
1 2 3 4 5 6 7 |
public class Main { public static void main(String[] args) { String str = "madam"; String reversed = new StringBuilder(str).reverse().toString(); System.out.println(str.equalsIgnoreCase(reversed)); } } |
1 2 3 4 |
def is_palindrome(s): return s.lower() == s[::-1].lower() print(is_palindrome("madam")) |
π 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
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Main { public static void main(String[] args) { int[] numbers = {10, 20, 45, 6, 78}; int max = numbers[0]; for (int num : numbers) { if (num > max) { max = num; } } System.out.println(max); } } |
1 2 3 4 5 6 7 8 |
def find_max(numbers): max_val = numbers[0] for num in numbers: if num > max_val: max_val = num return max_val print(find_max([10, 20, 45, 6, 78])) |
π 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
1 2 3 4 5 6 7 |
public class Main { public static void main(String[] args) { String sentence = "HCL is hiring software engineers"; String[] words = sentence.split(" "); System.out.println(words.length); } } |
1 2 3 4 5 |
def count_words(sentence): words = sentence.split() return len(words) print(count_words("HCL is hiring software engineers")) |
π 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
1 2 3 4 5 6 7 8 9 10 11 |
public class Main { public static void main(String[] args) { int n = 5, a = 0, b = 1; for (int i = 0; i < n; i++) { System.out.print(a + " "); int temp = a + b; a = b; b = temp; } } } |
1 2 3 4 5 6 7 |
def fibonacci(n): a, b = 0, 1 for _ in range(n): print(a, end=" ") a, b = b, a + b fibonacci(5) |
π 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
1 2 3 4 5 6 7 8 9 |
public class Main { public static void main(String[] args) { int n = 5, fact = 1; for (int i = 2; i <= n; i++) { fact *= i; } System.out.println(fact); } } |
1 2 3 4 5 6 7 |
def factorial(n): result = 1 for i in range(2, n+1): result *= i return result print(factorial(5)) |
π 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
1 2 3 4 5 6 7 8 |
public class Main { public static void main(String[] args) { int n = 10; for (int i = 2; i <= n; i += 2) { System.out.print(i + " "); } } } |
1 2 3 4 5 |
def print_even(n): for i in range(2, n+1, 2): print(i, end=" ") print_even(10) |
π 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
1 2 3 4 5 6 7 8 9 10 |
public class Main { public static void main(String[] args) { int n = 1234, rev = 0; while (n > 0) { rev = rev * 10 + n % 10; n /= 10; } System.out.println(rev); } } |
1 2 3 4 5 6 7 8 |
def reverse_number(n): rev = 0 while n > 0: rev = rev * 10 + n % 10 n //= 10 return rev print(reverse_number(1234)) |
π 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
1 2 3 4 5 6 7 |
public class Main { public static void main(String[] args) { int a = 5, b = 12, c = 9; int max = Math.max(a, Math.max(b, c)); System.out.println(max); } } |
1 2 3 4 |
def find_max(a, b, c): return max(a, b, c) print(find_max(5, 12, 9)) |
π 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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Main { public static void main(String[] args) { int n = 7, flag = 0; if (n < 2) flag = 1; for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) { flag = 1; break; } } System.out.println(flag == 0 ? "Prime" : "Not Prime"); } } |
1 2 3 4 5 6 7 8 9 |
def is_prime(n): if n < 2: return "Not Prime" for i in range(2, int(n**0.5)+1): if n % i == 0: return "Not Prime" return "Prime" print(is_prime(7)) |
π 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
1 2 3 4 5 6 7 8 9 10 |
public class Main { public static void main(String[] args) { int n = 123, sum = 0; while (n > 0) { sum += n % 10; n /= 10; } System.out.println(sum); } } |
1 2 3 4 5 6 7 8 |
def sum_of_digits(n): total = 0 while n > 0: total += n % 10 n //= 10 return total print(sum_of_digits(123)) |
π 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
1 2 3 4 5 6 7 8 9 10 11 |
public class Main { public static void main(String[] args) { int a = 24, b = 36; while (b != 0) { int temp = b; b = a % b; a = temp; } System.out.println(a); } } |
1 2 3 4 5 6 |
def gcd(a, b): while b: a, b = b, a % b return a print(gcd(24, 36)) |
π 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
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.*; public class Main { public static void main(String[] args) { Integer[] arr = {10, 20, 4, 45, 99, 99}; Set<Integer> set = new HashSet<>(Arrays.asList(arr)); List<Integer> list = new ArrayList<>(set); Collections.sort(list); System.out.println(list.get(list.size() - 2)); } } |
1 2 3 4 5 6 |
def second_largest(nums): unique_nums = list(set(nums)) unique_nums.sort() return unique_nums[-2] print(second_largest([10, 20, 4, 45, 99, 99])) |
π 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class Main { public static void main(String[] args) { String input = "HeLLo"; StringBuilder toggled = new StringBuilder(); for (char c : input.toCharArray()) { if (Character.isUpperCase(c)) toggled.append(Character.toLowerCase(c)); else toggled.append(Character.toUpperCase(c)); } System.out.println(toggled.toString()); } } |
1 2 3 4 |
def toggle_case(s): return s.swapcase() print(toggle_case("HeLLo")) |
π 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
1 2 3 4 5 6 7 8 9 |
public class Main { public static void main(String[] args) { int n = 10, sum = 0; for (int i = 2; i <= n; i += 2) { sum += i; } System.out.println(sum); } } |
1 2 3 4 5 6 7 |
def sum_even(n): total = 0 for i in range(2, n+1, 2): total += i return total print(sum_even(10)) |
π 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.*; public class Main { public static void main(String[] args) { int[] arr = {1, 2, 2, 3, 4, 3, 5}; Set<Integer> seen = new HashSet<>(); List<Integer> result = new ArrayList<>(); for (int num : arr) { if (!seen.contains(num)) { seen.add(num); result.add(num); } } System.out.println(result); } } |
1 2 3 4 5 6 7 8 9 10 |
def remove_duplicates(lst): result = [] seen = set() for num in lst: if num not in seen: seen.add(num) result.append(num) return result print(remove_duplicates([1, 2, 2, 3, 4, 3, 5])) |
π 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:
1 2 3 |
* *** ***** |
π£Code
1 2 3 4 5 6 7 8 9 |
public class Main { public static void main(String[] args) { int n = 3; for (int i = 0; i < n; i++) { System.out.print(" ".repeat(n - i - 1)); System.out.println("*".repeat(2 * i + 1)); } } } |
1 2 3 4 5 |
def print_pyramid(n): for i in range(n): print(' ' * (n - i - 1) + '*' * (2 * i + 1)) print_pyramid(3) |
π 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
1 2 3 4 5 6 7 |
public class Main { public static void main(String[] args) { String input = "HCL placement question"; String output = input.replace(" ", "_"); System.out.println(output); } } |
1 2 3 4 |
def replace_spaces(s): return s.replace(" ", "_") print(replace_spaces("HCL placement question")) |
π 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! πΌβ¨