Explore essential beginner and intermediate coding interview questions on data structures and algorithms, including arrays, linked lists, stacks, queues, and trees.
Which approach reverses an array in place without using extra space?
Explanation: Swapping elements from the ends moving inward reverses the array in place, using no extra space. Creating a new array does not work in place. Sorting produces a different result, not a reversed order. Replacing with zero removes all information from the array.
What is an efficient method to check if an array contains any duplicate elements?
Explanation: A hash set allows quick checks for repeated elements, providing efficient detection of duplicates. Summing elements or reversing does not identify duplicates. Sorting and removing even numbers unrelatedly reduces the array and may not help in duplicate detection.
Which algorithm efficiently checks if a singly linked list contains a cycle?
Explanation: Floyd's Tortoise and Hare uses two pointers moving at different speeds to detect cycles efficiently. Recursive traversal risks infinite loops if cycles exist. Counting nodes or summing values cannot reliably detect cycles.
How can a stack be used to check if a string of parentheses is balanced?
Explanation: A stack can track open parentheses and verify matches when closing ones appear, with emptiness indicating balance. Counting open parentheses ignores order. Reversing the string or summing ASCII values does not ensure correct pairing.
Which traversal of a binary search tree visits all nodes in ascending order of value?
Explanation: In-order traversal visits BST nodes in ascending order by visiting left subtree, then node, then right subtree. Pre-order and post-order visit nodes in different orders. Level-order uses breadth-first logic without guaranteeing numerical order.