Programming interview questions

Looking for good programming/interview questions?

Use the search below to find our solutions for selected questions!

All posts in Algorithms

3Sum challenge

Problem statement Given an array of integers, are there elements in such that ? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. For example, given array , A solution set is: Solution The idea behind the . . . Read more

Generate parentheses challenge

Problem statement Given pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given , a solution set is:

Solution

Longest palindromic substring challenge

Problem statement Given a string , find the longest palindromic substring in . You may assume that the maximum length of is . Sample input 1

Output 1

Sample input 1

Output 1

Solution We are going to iterate over each character in the input string . . . Read more

Group anagrams challenge

Problem statement Given an array of strings, group anagrams together. Sample input

Sample output

Note: All inputs will be in lower-case. Full code

Build power set of set

Problem statement Given a set of distinct integers, , return all possible subsets. Note: The solution set must not contain duplicate subsets. Sample input

Sample output

Full code

Swap list nodes in pairs challenge

Problem statement Given a linked list, swap every two adjacent nodes and return its head. For example: Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed. Full code . . . Read more

Container with most water challenge

Problem statement Given n non-negative integers , where each represents a point at coordinate . vertical lines are drawn such that the two endpoints of line is at and . Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You . . . Read more

Merge overlapping intervals challenge

Problem statement Given a set of intervals in any order merge the overlapping intervals and return the list of merged intervals. Sample input

Sample output

Solution The first step we are going to take is sort the intervals in ascending order based on their ending time. Next, we . . . Read more

KnightL on a chessboard challenge

Problem statement is a chess piece that moves in an shape. We define the possible moves of as any movement from some position to some satisfying either of the following: and , or and Note that and allow for the same exact set of movements. For example, the diagram below . . . Read more

Longest contiguous increasing subarray

Problem statement Given an array with numbers print a longest increasing subarray in . Sample Input

Sample Output

Solution We want to solve this problem in time complexity. The easiest approach is to to start iterating over our array and increase our sequence length as long as . . . . Read more