American Express
10+ HCL Group Interview Questions and Answers
Q1. Unique Frequency Problem Statement
Given a string 'STR' with lowercase letters, determine the minimum number of deletions required to ensure that every letter in the string appears a unique number of times.
Exa...read more
The task is to find the minimum number of deletions needed in a string to ensure each character appears a unique number of times.
Iterate through the string and count the frequency of each character
Track the frequencies in a hashmap
Identify the characters with duplicate frequencies and calculate the minimum deletions needed
Return the minimum number of deletions for each test case
Q2. Longest Switching Subarray Problem Statement
Determine the length of the longest contiguous subarray in a given array of positive integers, where the subarray qualifies as 'switching'. An array is defined as sw...read more
Find the length of the longest switching subarray in a given array of positive integers.
Iterate through the array and keep track of the longest switching subarray found so far.
Check if the numbers at even and odd positions are identical to determine a switching subarray.
Return the length of the longest switching subarray.
Example: For input [1, 4, 1, 4, 3, 2, 3, 0], the longest switching subarray is [1, 4, 1, 4] with length 4.
Q3. Palindrome Linked List Detection
Given a singly linked list of integers, determine if it is a palindrome. A linked list is considered a palindrome if it reads the same forward and backward.
Example:
Input:
The ...read more
Check if a singly linked list is a palindrome by comparing elements from both ends.
Traverse the linked list to find the middle point
Reverse the second half of the linked list
Compare the first half with the reversed second half to check for palindrome
Example: Input: 1 2 3 2 1 -1, Output: true
Q4. Travelling Salesman Problem
Given a list of cities numbered from 0 to N-1 and a matrix DISTANCE
consisting of 'N' rows and 'N' columns, representing the distances between each pair of cities, find the shortest ...read more
Q5. Sliding Maximum Problem Statement
Given an array of integers ARR
of length 'N' and a positive integer 'K', find the maximum elements for each contiguous subarray of size K.
Example:
Input:
ARR = [3, 4, -1, 1, 5...read more
Implement a function to find maximum elements for each contiguous subarray of size K in an array of integers.
Iterate through the array and maintain a deque to store the indices of elements in decreasing order.
Pop elements from the deque that are out of the current window and add the maximum element to the result array.
Return the result array containing maximum elements for each subarray of size K.
Q6. Longest Substring with At Most K Distinct Characters
Given a string S
of length N
and an integer K
, find the length of the longest substring that contains at most K
distinct characters.
Input:
The first line co...read more
Find the length of the longest substring with at most K distinct characters in a given string.
Use a sliding window approach to keep track of the characters and their counts in the substring.
Maintain a hashmap to store the characters and their frequencies within the window.
Update the window size and characters count as you iterate through the string.
Keep track of the longest substring length with at most K distinct characters.
Return the length of the longest substring for each...read more
Q7. Interval List Intersection Problem
You are provided with two sorted lists of closed intervals, INTERVAL1
and INTERVAL2
. A closed interval [x, y] (x < y) signifies the set of real numbers z such that x <= z <= y...read more
Find the intersections of two sorted lists of closed intervals.
Iterate through both interval lists to find intersections
Compare the intervals and find the common range
Handle cases where there is no intersection between intervals
Q8. Stack using Two Queues Problem Statement
Develop a Stack Data Structure to store integer values using two Queues internally.
Your stack implementation should provide these public functions:
Explanation:
1. Cons...read more
Implement a stack using two queues to store integer values with specified functions.
Use two queues to simulate stack operations efficiently.
Maintain one queue for storing elements and another for temporary storage during push operation.
Ensure proper handling of edge cases like empty stack or invalid operations.
Example: Push operation involves transferring elements from one queue to another before adding the new element.
Example: Pop operation removes and returns the top elemen...read more
Q9. Minimum Number of Swaps to Sort an Array
Find the minimum number of swaps required to sort a given array of distinct elements in ascending order.
Input:
T (number of test cases)
For each test case:
N (size of the...read more
The minimum number of swaps required to sort a given array of distinct elements in ascending order.
Use a graph-based approach to find cycles in the array
Count the number of swaps needed to fix each cycle
Sum up the swaps needed for all cycles to get the total minimum swaps
Q10. Maximum Non-Adjacent Subsequence Sum
Given an array of integers, determine the maximum sum of a subsequence without choosing adjacent elements in the original array.
Input:
The first line consists of an integer...read more
Find the maximum sum of a subsequence without choosing adjacent elements in an array.
Use dynamic programming to keep track of the maximum sum at each index, considering whether to include or exclude the current element.
At each index, the maximum sum is either the sum of the current element and the sum two positions back, or the sum at the previous index.
Iterate through the array and update the maximum sum accordingly.
Example: For input [3, 2, 7, 10], the maximum non-adjacent ...read more
Q11. Detect and Remove Loop in Linked List
For a given singly linked list, identify if a loop exists and remove it, adjusting the linked list in place. Return the modified linked list.
Expected Complexity:
Aim for a...read more
Detect and remove loop in a singly linked list in place with O(n) time complexity and O(1) space complexity.
Use Floyd's Cycle Detection Algorithm to identify the loop in the linked list.
Once the loop is detected, use two pointers to find the start of the loop.
Adjust the pointers to remove the loop and return the modified linked list.
Q12. Left View of a Binary Tree Problem Statement
Given a binary tree, your task is to print the left view of the tree.
Example:
Input:
The input will be in level order form, with node values separated by a space. U...read more
Print the left view of a binary tree given in level order form.
Traverse the tree level by level and print the first node encountered at each level
Use a queue to perform level order traversal
Keep track of the current level and the maximum level seen so far
More about working at American Express
Top Software Developer Intern Interview Questions from Similar Companies
Reviews
Interviews
Salaries
Users/Month