Interview Questions and Answers
Freshers / Beginner level questions & answers
Ques 1. Fibonacci Series
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones.
Example:
Fib(0) = 0, Fib(1) = 1, Fib(n) = Fib(n-1) + Fib(n-2)
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 2. Maximum Subarray Sum
Find the contiguous subarray with the largest sum.
Example:
Input: [-2, 1, -3, 4, -1, 2, 1, -5, 4], Output: 6 (subarray [4, -1, 2, 1])
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 3. Unique Paths
A robot is located at the top-left corner of a m x n grid. It can only move either down or right. Find the number of unique paths to reach the bottom-right corner.
Example:
Input: m = 3, n = 7, Output: 28
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Intermediate / 1 to 5 years experienced level questions & answers
Ques 4. Longest Increasing Subsequence (LIS)
Find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order.
Example:
Input: [10, 22, 9, 33, 21, 50, 41, 60, 80], Output: 6
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 5. Coin Change Problem
Given a set of coin denominations, find the number of ways to make a certain amount of change.
Example:
Input: coins=[1, 2, 5], amount=5, Output: 4
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 6. 0/1 Knapsack Problem
Given weights and values of items, find the maximum value that can be obtained by selecting a subset of items with total weight not exceeding a given limit.
Example:
Weights: [2, 3, 4, 5], Values: [3, 4, 5, 6], Knapsack Capacity: 5, Output: 11
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 7. Longest Common Subsequence (LCS)
Given two sequences, find the length of the longest subsequence present in both of them.
Example:
Input: ABCBDAB, BDCAB, Output: 4 (BCAB)
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 8. Rod Cutting Problem
Given a rod of length n and prices at which different lengths of the rod can be sold, find the maximum value obtainable by cutting up the rod.
Example:
Lengths: [1, 2, 3, 4, 5], Prices: [1, 5, 8, 9, 10], Rod Length: 4, Output: 10
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 9. Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) that has the largest product.
Example:
Input: [2, 3, -2, 4], Output: 6 (subarray [2, 3])
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 10. Decode Ways
A message containing letters from A-Z can be encoded into numbers. Given a non-empty string containing only digits, determine the total number of ways to decode it.
Example:
Input: "226", Output: 3 ("22" can be decoded as "BB", "2" + "2" + "6" can be decoded as "VVF")
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 11. Word Break Problem
Given a non-empty string and a dictionary of words, determine if the string can be segmented into a space-separated sequence of one or more dictionary words.
Example:
Input: s = "leetcode", wordDict = ["leet", "code"], Output: true
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 12. Minimum Path Sum
Given a grid filled with non-negative numbers, find a path from the top-left to the bottom-right corner that minimizes the sum of numbers along the path.
Example:
Grid: [[1,3,1],[1,5,1],[4,2,1]], Output: 7 (1 -> 3 -> 1 -> 1 -> 1)
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 13. Longest Palindromic Substring
Given a string, find the longest substring which is a palindrome.
Example:
Input: "babad", Output: "bab" or "aba"
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 14. Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.
Example:
Input: [1, 5, 11, 5], Output: true (subset sums: [1, 5, 5] and [11])
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 15. Maximum Length of Repeated Subarray
Given two integer arrays, find the maximum length of a common subarray of both arrays.
Example:
Input: A = [1,2,3,2,1], B = [3,2,1,4,7], Output: 3 ([3, 2, 1])
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 16. Palindromic Substrings
Given a string, find the total number of palindromic substrings.
Example:
Input: "abc", Output: 3 ("a", "b", "c")
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 17. Minimum Cost Path
Given a 2D grid, find the minimum cost path from the top-left corner to the bottom-right corner.
Example:
Grid: [[1,3,1],[1,5,1],[4,2,1]], Output: 7 (1 -> 3 -> 1 -> 1 -> 1)
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 18. Arithmetic Slices
A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. Return the total number of arithmetic slices in the array.
Example:
Input: [1, 2, 3, 4], Output: 3 (three arithmetic slices: [1, 2, 3], [2, 3, 4], [1, 2, 3, 4])
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 19. Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
Example:
Matrix: [[1, 0, 1, 0, 0], [1, 0, 1, 1, 1], [1, 1, 1, 1, 1], [1, 0, 0, 1, 0]], Output: 4
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 20. Paint House
There are a row of n houses, each house can be painted with one of the three colors: red, blue, or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.
Example:
Costs: [[17, 2, 17], [16, 16, 5], [14, 3, 19]], Output: 10 (paint the first house blue, the second house green, and the third house blue)
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 21. Number of Dice Rolls With Target Sum
You have d dice, and each die has f faces numbered 1 to f. Return the number of possible ways (out of f^d total ways) to roll the dice so the sum of the face-up numbers equals target.
Example:
d = 2, f = 6, target = 7, Output: 6 (possible rolls: [1,6], [2,5], [3,4], [4,3], [5,2], [6,1])
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Experienced / Expert level questions & answers
Ques 22. Edit Distance
Given two strings, find the minimum number of operations required to convert one string into the other.
Example:
Input: word1 = "horse", word2 = "ros", Output: 3
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 23. Palindrome Partitioning
Given a string, partition it into as many palindromic substrings as possible.
Example:
Input: aab, Output: [['a', 'a', 'b'], ['aa', 'b']]
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 24. Count Palindromic Subsequences
Given a string, find the number of distinct palindromic subsequences of it.
Example:
Input: "bccb", Output: 6 ("b", "c", "c", "b", "ccb", "bccb")
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 25. Longest Increasing Path in a Matrix
Given an integer matrix, find the length of the longest increasing path.
Example:
Matrix: [[9,9,4],[6,6,8],[2,1,1]], Output: 4 (9 -> 6 -> 8 -> 1)
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 26. RegularExpression Matching
Implement regular expression matching with support for '.' and '*'.
Example:
Input: s = "aa", p = "a*", Output: true
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 27. Maximum Profit with K Transactions
Given an array representing stock prices, find the maximum profit that can be obtained with at most k transactions.
Example:
Input: [3, 2, 6, 5, 0, 3], k = 2, Output: 7
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 28. Count Distinct Subsequences
Given a string, find the number of distinct non-empty subsequences of the string.
Example:
Input: "abc", Output: 7 ("a", "b", "c", "ab", "ac", "bc", "abc")
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 29. Word Break II
Given a non-empty string and a dictionary of words, return all possible sentences formed by concatenating words from the dictionary.
Example:
Input: s = "catsanddog", wordDict = ["cat", "cats", "and", "sand", "dog"], Output: ["cats and dog", "cat sand dog"]
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 30. Word Ladder II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequences from beginWord to endWord.
Example:
beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"], Output: [["hit","hot","dot","dog","cog"],["hit","hot","lot","log","cog"]]
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Most helpful rated by users: