Why does the JDK documentation recommend ArrayDeque over java.util.Stack for a stack?
ArrayDeque is an unsynchronized circular array (and it does not allow null elements).
Data Structures in Java · Exercise 1 of 4
Twenty multiple-choice questions on data structures in Java: Big-O, dynamic arrays, linked lists, stacks, queues, circular queues, heaps, binary search trees, hash tables, graphs and tries. Pick an option to check your answer instantly.
Why does the JDK documentation recommend ArrayDeque over java.util.Stack for a stack?
ArrayDeque is an unsynchronized circular array (and it does not allow null elements).
In what order does a for-each loop visit the elements of a PriorityQueue?
Only poll removes elements in priority order; the iterator walks the heap array.
A hash table resolves a collision by trying the next slot, then the next, until it finds a free one. What is this technique called?
HashMap uses separate chaining instead; IdentityHashMap and ThreadLocal's map use linear probing.
How much memory does an adjacency list need for a graph with V vertices and E edges?
One list per vertex plus one entry per edge (two for an undirected edge). A matrix needs O(V²).
A trie holds n words. What is the cost of checking whether a word of length L is present?
The search follows one child per character, regardless of how many words are stored.
In an array-based circular queue of size capacity, how is the next tail index computed?
The modulo wraps the index from the last slot back to 0.
You already hold a reference to a node in a doubly linked list. What does it cost to remove that node?
Point node.prev.next at node.next and node.next.prev at node.prev. A singly linked list would first need to find the previous node: O(n).
What is the worst-case time of PriorityQueue.offer with n elements already in the queue?
The new element sifts up at most the height of a complete binary tree, about log₂ n levels.
Which technique detects a cycle in a singly linked list using only O(1) extra memory?
If there is a loop, the fast pointer (two steps) eventually laps the slow one (one step). The HashSet approach works but uses O(n) memory.
new PriorityQueue<>(collection) builds the heap with bottom-up heapify. What is its time complexity?
Most nodes are near the bottom and sift down only a level or two; the total work sums to O(n), better than n separate offers.
Timed online tests with instant scores and explanations.