Interview Q&A / Data Structures & Algorithms
Data Structures & Algorithms interview questions
Arrays, linked lists, hashing, trees and patterns, with line-by-line code and complexity.
Prefer rapid revision? Practise Data Structures & Algorithms short interview answers →
- How do you find the longest substring without repeating characters?
Use a sliding window. Expand the right end one character at a time and remember the last index where each character was seen. If the new character already appears inside the window, move the left end just past its previous position. Track the largest window size. This is O(n) time.
AdvancedIn-depthCoding4 min read - How do you design an LRU cache with O(1) get and put?
Combine a hash map for instant lookup with an ordering of recent use. In JavaScript a Map remembers insertion order, so deleting and re-inserting a key on each access moves it to the newest end; the oldest key is evicted when full.
AdvancedQuick answerCoding1 min read - How do you find the maximum in every sliding window of size k?
Keep a deque of indices whose values are in decreasing order. Drop indices that left the window from the front, drop smaller values from the back before adding the new index, and the front is always the window maximum.
AdvancedQuick answerCoding1 min read - How do you find the node where a cycle begins in a linked list?
Detect the cycle with slow and fast pointers. When they meet, reset one pointer to the head and move both one step at a time. They meet again exactly at the start of the cycle.
AdvancedQuick answerCoding1 min read - How do you solve the N-Queens problem with backtracking?
Place one queen per row. For each column in the row, check that no earlier queen shares the column or a diagonal. If safe, place it and recurse to the next row; otherwise skip. Undo and try the next column.
AdvancedQuick answerCoding1 min read