Data structures and algorithms
100 of the coding questions asked most often in interviews, each with a full solution in Java, grouped by the pattern that solves them, so you can practise one technique at a time. Reported by candidates in public interview write-ups and reworded by Scrivio. How the reports are counted
Your progress: 0 of 100 done, 0 saved
By pattern
In the order most people learn themBy data structure
Most asked
Most reported firstWrite code that reverses a singly linked list.
Reported 156 times
Read the solutionLinked listsExample sources
3 of the write-ups counted: geeksforgeeks.org 2017, geeksforgeeks.org 2023, geeksforgeeks.org 2016
Detect whether a singly linked list contains a loop.
Reported 119 times
Read the solutionLinked listsExample sources
3 of the write-ups counted: geeksforgeeks.org 2021, geeksforgeeks.org 2021, geeksforgeeks.org 2019
Find the contiguous subarray with the largest sum.
Reported 92 times
Read the solutionDynamic programmingExample sources
3 of the write-ups counted: geeksforgeeks.org 2018, geeksforgeeks.org 2018, geeksforgeeks.org 2017
Return the middle node of a singly linked list.
Reported 80 times
Read the solutionLinked listsExample sources
3 of the write-ups counted: geeksforgeeks.org 2014, geeksforgeeks.org 2012, geeksforgeeks.org 2021
Build a stack using only queue operations.
Reported 73 times
Read the solutionStacks and queuesExample sources
3 of the write-ups counted: geeksforgeeks.org 2022, geeksforgeeks.org 2019, geeksforgeeks.org 2017
How does an array differ from a linked list?
Reported 70 times
Read the solutionData structure basicsExample sources
3 of the write-ups counted: geeksforgeeks.org 2023, geeksforgeeks.org 2022, geeksforgeeks.org 2018
Design and implement an LRU cache whose get and put both run in O(1) time.
Reported 69 times
Read the solutionDesigning data structuresExample sources
3 of the write-ups counted: geeksforgeeks.org 2021, geeksforgeeks.org 2019, geeksforgeeks.org 2018
Find the longest substring of a string that reads the same forwards and backwards.
Reported 69 times
Read the solutionStrings and triesExample sources
3 of the write-ups counted: geeksforgeeks.org 2020, geeksforgeeks.org 2016, geeksforgeeks.org 2020
Search for an element in a sorted array that has been rotated.
Reported 60 times
Read the solutionBinary searchExample sources
3 of the write-ups counted: geeksforgeeks.org 2020, geeksforgeeks.org 2020, geeksforgeeks.org 2017
Merge two sorted linked lists into one sorted linked list.
Reported 59 times
Read the solutionLinked listsExample sources
3 of the write-ups counted: geeksforgeeks.org 2021, geeksforgeeks.org 2021, geeksforgeeks.org 2020
Reverse the nodes of a linked list in groups of k.
Reported 59 times
Read the solutionLinked listsExample sources
3 of the write-ups counted: geeksforgeeks.org 2019, geeksforgeeks.org 2017, geeksforgeeks.org 2017
Find the k-th largest element in an unsorted array.
Reported 55 times
Read the solutionHeaps and top kExample sources
3 of the write-ups counted: geeksforgeeks.org 2020, geeksforgeeks.org 2022, geeksforgeeks.org 2020
Count the islands of connected 1s in a binary grid.
Reported 54 times
Read the solutionGraph traversalExample sources
3 of the write-ups counted: geeksforgeeks.org 2023, geeksforgeeks.org 2022, geeksforgeeks.org 2017
How does a stack differ from a queue?
Reported 53 times
Read the solutionData structure basicsExample sources
3 of the write-ups counted: geeksforgeeks.org 2024, geeksforgeeks.org 2024, geeksforgeeks.org 2023
Find the second largest element in an array.
Reported 52 times
Read the solutionArrays and hashingExample sources
3 of the write-ups counted: geeksforgeeks.org 2014, geeksforgeeks.org 2021, geeksforgeeks.org 2014
Detect a loop in a linked list and remove it.
Reported 47 times
Read the solutionLinked listsExample sources
3 of the write-ups counted: geeksforgeeks.org 2016, geeksforgeeks.org 2024, geeksforgeeks.org 2024
Print the nodes visible from the left side of a binary tree.
Reported 47 times
Read the solutionTreesExample sources
3 of the write-ups counted: geeksforgeeks.org 2019, geeksforgeeks.org 2018, geeksforgeeks.org 2016
Merge two sorted arrays into one sorted array.
Reported 47 times
Read the solutionTwo pointersExample sources
3 of the write-ups counted: geeksforgeeks.org 2019, geeksforgeeks.org 2019, geeksforgeeks.org 2017
Find the lowest common ancestor of two nodes in a binary tree.
Reported 46 times
Read the solutionTreesExample sources
3 of the write-ups counted: geeksforgeeks.org 2020, geeksforgeeks.org 2024, geeksforgeeks.org 2015
Reported 46 times
Read the solutionSorting and intervalsExample sources
3 of the write-ups counted: geeksforgeeks.org 2019, geeksforgeeks.org 2018, geeksforgeeks.org 2024