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 palindromic substring in a string?
Every palindrome has a centre. Try each index as an odd-length centre and each gap between letters as an even-length centre, expand outward while the ends match, and keep the longest.
IntermediateQuick answerCoding1 min read - How do you generate all valid combinations of n pairs of parentheses?
Build the string character by character. You may add an opening bracket while fewer than n have been used, and a closing bracket only when it would not exceed the number of openings. A string of length 2n is valid.
IntermediateQuick answerCoding1 min read - How do you group anagrams together from a list of words?
Anagrams share the same sorted letters, so use the sorted word as a hash map key and collect each word under its key. The map values are the groups.
IntermediateQuick answerCoding1 min read