Programming Question Set
Question 1: Minimum Steps to One
Given an integer n, find the minimum number of steps required to reduce n to 1.
You can perform any of the following three operations:
- If n is divisible by 3, divide it by 3.
- If n is divisible by 2, divide it by 2.
- Subtract 1 from n.
Example:
Input: n = 10
Output: 3 (10 -> 9 -> 3 -> 1)
Question 2: Unique Paths in a Grid
You are given two integers m and n, representing the dimensions of an m x n grid. You start at the
top-left corner and can only move down or right at any point in time. Find the number of unique
paths to reach the bottom-right corner of the grid.
Example:
Input: m = 3, n = 7
Output: 28
Question 3: Largest Rectangle in Histogram
Given an array representing the heights of bars in a histogram where the width of each bar is 1, find
the area of the largest rectangle that can be formed in the histogram.
Example:
Input: heights = [2,1,5,6,2,3]
Output: 10 (formed by bars of height 5 and 6)
Question 4: Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
Your algorithm should run in O(n) time.
Example:
Input: nums = [100, 4, 200, 1, 3, 2]
Output: 4 (The longest consecutive sequence is [1, 2, 3, 4])
Question 5: Valid Parentheses
Given a string containing only the characters '(', ')', '{', '}', '[' and ']', determine if the input string is
valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
Example:
Input: s = '([{}])'
Output: true
Input: s = '(]'
Output: false
Question 6: Container With Most Water
Given an array of integers height where each element represents the height of a vertical line on the
x-axis. Find two lines, which together with the x-axis forms a container, such that the container
contains the most water.
Example:
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49 (formed by lines at indices 1 and 8)
Question 7: Binary Tree Level Order Traversal
Given the root of a binary tree, return the level order traversal of its nodes' values (i.e., from left to
right, level by level).
Example:
Input: root = [3,9,20,null,null,15,7]
Output: [[3], [9,20], [15,7]]
Question 8: Merge Intervals
Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals and
return an array of the non-overlapping intervals that cover all the intervals in the input.
Example:
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Question 9: Find All Anagrams in a String
Given two strings s and p, return an array of all the start indices of p's anagrams in s.
Example:
Input: s = 'cbaebabacd', p = 'abc'
Output: [0,6]
Question 10: Kth Smallest Element in a Sorted Matrix
Given an n x n matrix where each of the rows and columns is sorted in ascending order, return the
k-th smallest element in the matrix.
Example:
Input: matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
Output: 13