Hexaware Coding Questions
Here are some Hexaware coding questions for your assessment preparation:
1. Given a list of strings, find the longest word with an odd length. If there is no such word, return "Better luck next time".
Input
5
Hello Good morning Welcome you
Output
morning
C
#include <stdio.h>
#include <string.h>
int main() {
int n;
scanf("%d", &n);
char word[100], result[100] = "";
while (n--) {
scanf("%s", word);
if (strlen(word) % 2 == 1 && strlen(word) > strlen(result)) {
strcpy(result, word);
}
}
if (strlen(result) == 0)
printf("Better luck next time\n");
else
printf("%s\n", result);
return 0;
}
Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String result = "";
for (int i = 0; i < n; i++) {
String word = sc.next();
if (word.length() % 2 == 1 && word.length() > result.length()) {
result = word;
}
}
System.out.println(result.isEmpty() ? "Better luck next time" : result);
}
}
Python
n = int(input())
words = input().split()
max_word = ""
for word in words:
if len(word) % 2 == 1 and len(word) > len(max_word):
max_word = word
print(max_word if max_word else "Better luck next time")
2. Calculate the maximum profit from stock prices over several days. You can only buy and sell once.
Example
Input
7
1 9 2 11 1 9 2
Output
10
C
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int prices[n];
for (int i = 0; i < n; i++)
scanf("%d", &prices[i]);
int min_price = prices[0];
int max_profit = 0;
for (int i = 1; i < n; i++) {
if (prices[i] < min_price) {
min_price = prices[i];
} else {
int profit = prices[i] - min_price;
if (profit > max_profit) {
max_profit = profit;
}
}
}
printf("%d\n", max_profit);
return 0;
}
Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] prices = new int[n];
for (int i = 0; i < n; i++) {
prices[i] = sc.nextInt();
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] prices = new int[n];
for (int i = 0; i < n; i++) {
prices[i] = sc.nextInt();
}
int minPrice = prices[0];
int maxProfit = 0;
for (int i = 1; i < n; i++) {
if (prices[i] < minPrice) {
minPrice = prices[i];
} else {
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
}
}
System.out.println(maxProfit);
}
}
Python
n = int(input())
prices = list(map(int, input().split()))
min_price = prices[0]
max_profit = 0
for price in prices[1:]:
if price < min_price:
min_price = price
else:
max_profit = max(max_profit, price - min_price)
print(max_profit)
3. Given a string representation of frogs and stones, count the number of frogs between specified stone indices.
Example
Input
|**|*|
1
1 5
Output
2
C
#include <stdio.h>
#include <string.h>
int main() {
char s[100];
scanf("%s", s);
int n;
scanf("%d", &n);
while (n--) {
int start, end;
scanf("%d %d", &start, &end);
int count = 0;
for (int i = start - 1; i <= end - 1; i++) {
if (s[i] == '*') {
count++;
}
}
printf("%d\n", count);
}
return 0;
}
Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
int n = sc.nextInt();
while (n-- > 0) {
int start = sc.nextInt();
int end = sc.nextInt();
int count = 0;
for (int i = start - 1; i < end; i++) {
if (s.charAt(i) == '*') {
count++;
}
}
System.out.println(count);
}
}
}
Python
s = input()
n = int(input())
for _ in range(n):
start, end = map(int, input().split())
count = s[start-1:end].count('*')
print(count)
4. Given a string representation of apples and baskets, count the number of apples between specified basket indices. The string consists of 'A' (apple) and 'B' (basket).
Example
Input
5
1 2 3 4 5
1 4
Output
2
C
#include <stdio.h>
int main() {
int n, count = 0, start, end;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
scanf("%d %d", &start, &end);
for (int i = start + 1; i < end; i++) {
if (arr[i] % 2 != 0) count++;
}
printf("%d\n", count);
return 0;
}
Java
import java.util.Scanner;
public class CountOddNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
int start = scanner.nextInt();
int end = scanner.nextInt();
int count = 0;
for (int i = start + 1; i < end; i++) {
if (arr[i] % 2 != 0) count++;
}
System.out.println(count);
scanner.close();
}
}
Python
n = int(input())
arr = list(map(int, input().split()))
start, end = map(int, input().split())
count = 0
for i in range(start + 1, end):
if arr[i] % 2 != 0:
count += 1
print(count)
5. Given a list of characters, count the number of unique characters in the specified range.
Example
Input
5
a b a c d
1 4
Output
3
C
#include <stdio.h>
#include <stdbool.h>
int main() {
int n, start, end, uniqueCount = 0;
scanf("%d", &n);
char arr[n];
for (int i = 0; i < n; i++) {
scanf(" %c", &arr[i]);
}
scanf("%d %d", &start, &end);
bool seen[256] = {false};
for (int i = start + 1; i < end; i++) {
if (!seen[arr[i]]) {
seen[arr[i]] = true;
uniqueCount++;
}
}
printf("%d\n", uniqueCount);
return 0;
}
Java
import java.util.HashSet;
import java.util.Scanner;
public class CountUniqueCharacters {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
char[] arr = new char[n];
for (int i = 0; i < n; i++) {
arr[i] = scanner.next().charAt(0);
}
int start = scanner.nextInt();
int end = scanner.nextInt();
HashSet<Character> uniqueChars = new HashSet<>();
for (int i = start + 1; i < end; i++) {
uniqueChars.add(arr[i]);
}
System.out.println(uniqueChars.size());
scanner.close();
}
}
Python
n = int(input())
arr = input().split()
start, end = map(int, input().split())
unique_chars = set()
for i in range(start + 1, end):
unique_chars.add(arr[i])
print(len(unique_chars))
6. Check if there are adjacent baskets ('B') in a given string of apples and baskets.
Example
Input
ABABBA
Output
Yes
C
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
scanf("%s", str);
for (int i = 0; str[i + 1]; i++) {
if (str[i] == 'B' && str[i + 1] == 'B') {
printf("Yes\n");
return 0;
}
}
printf("No\n");
return 0;
}
Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
for (int i = 0; i < str.length() - 1; i++) {
if (str.charAt(i) == 'B' && str.charAt(i + 1) == 'B') {
System.out.println("Yes");
return;
}
}
System.out.println("No");
}
}
Python
str_input = input()
if 'BB' in str_input:
print("Yes")
else:
print("No")
7. Given an array of integers, find the first repeated element within the specified range.
Example
Input
7
1 2 3 4 2 5 1
0 6
Output
2
C
#include <stdio.h>
#include <stdbool.h>
int main() {
int n, start, end;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
scanf("%d %d", &start, &end);
bool seen[100] = {false}; // Adjust size if necessary
for (int i = start; i < end; i++) {
if (seen[arr[i]]) {
printf("%d\n", arr[i]);
return 0;
}
seen[arr[i]] = true;
}
printf("-1\n");
return 0;
}
Java
import java.util.HashSet;
import java.util.Scanner;
public class FirstRepeatedElement {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
int start = scanner.nextInt();
int end = scanner.nextInt();
HashSet<Integer> seen = new HashSet<>();
for (int i = start; i < end; i++) {
if (seen.contains(arr[i])) {
System.out.println(arr[i]);
return;
}
seen.add(arr[i]);
}
System.out.println("-1");
scanner.close();
}
}
Python
n = int(input())
arr = list(map(int, input().split()))
start, end = map(int, input().split())
seen = set()
for i in range(start, end):
if arr[i] in seen:
print(arr[i])
break
seen.add(arr[i])
else:
print(-1)
8. Given an array of integers, reverse the elements in the subarray between specified indices.
Example
Input
5
1 2 3 4 5
1 4
Output
1 4 3 2 5
C
#include <stdio.h>
int main() {
int n, start, end;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
scanf("%d %d", &start, &end);
while (start < end) {
int temp = arr[start];
arr[start] = arr[end - 1];
arr[end - 1] = temp;
start++;
end--;
}
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Java
import java.util.Scanner;
public class ReverseSubarray {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
int start = scanner.nextInt();
int end = scanner.nextInt();
while (start < end) {
int temp = arr[start];
arr[start] = arr[end - 1];
arr[end - 1] = temp;
start++;
end--;
}
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
scanner.close();
}
}
Python
n = int(input())
arr = list(map(int, input().split()))
start, end = map(int, input().split())
while start < end:
arr[start], arr[end - 1] = arr[end - 1], arr[start]
start += 1
end -= 1
print(" ".join(map(str, arr)))
9. Given an array of integers from 1 to n with one number missing, find the missing number.
Example
Input
5
1 2 4 5
Output
3
C
#include <stdio.h>
int main() {
int n, sum = 0;
scanf("%d", &n);
int arr[n - 1];
for (int i = 0; i < n - 1; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
int total = n * (n + 1) / 2;
printf("%d\n", total - sum);
return 0;
}
Java
import java.util.Scanner;
public class MissingNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int sum = 0;
for (int i = 0; i < n - 1; i++) {
sum += scanner.nextInt();
}
int total = n * (n + 1) / 2;
System.out.println(total - sum);
scanner.close();
}
}
Python
n = int(input())
arr = list(map(int, input().split()))
total = n * (n + 1) // 2
sum_arr = sum(arr)
print(total - sum_arr)
Tips for Cracking Hexaware Coding Round
Here are some tips for tackling Hexaware coding questions:
- Master fundamental data structures and algorithms.
- Use platforms like LeetCode and HackerRank to solve coding problems regularly.
- Familiarize yourself with problem-solving strategies like dynamic programming and backtracking.
- Understand the problem statement properly before coding.
- Outline your approach and sketch algorithms before implementation.
- Use meaningful variable names and add comments for clarity.
- Check your code with various test cases, including edge cases.
- Keep track of time; don’t spend too long on one problem.
- Approach challenges systematically; don’t panic.
- Look at Hexaware’s previous coding questions for insights into the assessment format.
Conclusion
In conclusion, a well-rounded preparation for the Hexaware coding round will help you clear your assessment. By familiarizing yourself with the types of questions asked, practicing regularly, and improving your problem-solving skills, you can enhance your chances of success.
Frequently Asked Questions
1. What should I expect in the Hexaware coding questions for PGET?
Expect a combination of algorithmic challenges and real-world problem-solving tasks that test your coding proficiency and logical reasoning.
2. What coding languages should I be familiar with for Hexaware technologies coding questions?
Familiarity with languages such as C, Java, and Python is beneficial, as many questions may be framed around these languages.