30 Essential Coding Interview Questions for Arrays, Strings, Linked Lists

 Arrays:

1)Find the second smallest element in an array.

Hint: Iterate through the array while keeping track of the two smallest elements.

2)Rearrange positive and negative numbers in an array.

Hint: Use two pointers to swap elements to separate positive and negative numbers.

3)Find the subarray with the maximum sum.

Hint: Use Kadane's algorithm to find the maximum sum subarray.

4)Remove duplicates from an unsorted array.

Hint: Use a hash set or hash map to keep track of unique elements.

5)Rearrange an array in alternating positive and negative numbers.

Hint: Use two pointers to swap elements while maintaining the alternating pattern.

6)Find the maximum product of two integers in an array.

Hint: Keep track of the maximum and second maximum elements while iterating through the array.

7)Determine if an array is a rotation of another array.

Hint: Concatenate the first array with itself and check if the second array is a subarray of the concatenated array.

8)Find the majority element in an array.

Hint: Use the Moore's Voting Algorithm to find the majority element.

9)Find the kth largest element in an array.

Hint: Use quickselect algorithm, which is similar to quicksort but terminates once the pivot is in its sorted position.

10)Find all pairs of elements in an array that add up to a given sum.

Hint: Use a hash set or hash map to store the complement of each element while iterating through the array.


Strings:

1)Check if two strings are anagrams.

Hint: Compare the sorted versions of the two strings.

2)Find the longest substring without repeating characters.

Hint: Use a sliding window approach with a hash set to track unique characters.

3)Reverse words in a string.

Hint: Reverse the entire string, then reverse each word individually.

4)Determine if a string is a palindrome.

Hint: Compare characters from both ends of the string while ignoring non-alphanumeric characters.

5)Compress a string by replacing repeated characters with a count.

Hint: Iterate through the string and maintain a count of repeated characters.

6)Find the first non-repeated character in a string.

Hint: Use a hash map to count the occurrences of each character.

7)Count the occurrences of a substring in a larger string.

Hint: Use the Knuth-Morris-Pratt (KMP) algorithm for pattern matching.

8)Check if a string is a rotation of another string.

Hint: Concatenate the first string with itself and check if the second string is a substring of the concatenated string.

9)Convert a string to an integer.

Hint: Handle sign, ignore leading whitespace, and convert digit characters to numbers.

10)Determine if a string is a valid palindrome after deleting at most one character.

Hint: Use two pointers to compare characters from both ends, and skip one character if a mismatch is encountered.


Linked Lists:

1)Reverse a linked list.

Hint: Use three pointers to reverse the links between nodes iteratively or recursively.

2)Find the middle element of a linked list.

Hint: Use the "runner" technique with two pointers, one moving at twice the speed of the other.

3)Detect if a linked list has a cycle.

Hint: Use the "runner" technique with two pointers, one moving at twice the speed of the other, and check for a meeting point.

4)Merge two sorted linked lists.

Hint: Use a dummy node and iterate through both lists, comparing and connecting nodes.

5)Delete a node from a linked list given only access to that node.

Hint: Copy the data from the next node to the current node and update the pointers to skip the next node.

6)Find the kth node from the end of a linked list.

Hint: Use two pointers, with one pointer k steps ahead of the other.

7)Check if a linked list is a palindrome.

Hint: Reverse the second half of the linked list and compare it with the first half.

8)Remove duplicates from a linked list.

Hint: Use a hash set or hash map to keep track of unique elements while iterating through the linked list.

9)Sort a linked list in ascending order.

Hint: Use a sorting algorithm like merge sort or insertion sort tailored for linked lists.

10)Split a linked list into two halves.

Hint: Use the "runner" technique with two pointers, one moving at twice the speed of the other, to find the midpoint and split the list.

Comments

Popular Posts