Practice — Hash Maps and Sets (5 questions)
Pair Sum - Unsorted
Given an unsorted array of integers nums and an integer target,
return the indices of the two numbers that add up to target.
You may assume that each input has exactly one valid answer, and you may not use the same element twice. Return the indices in any order.
Example 1
Input: nums = [3, 7, 1, 9], target = 8
Output: [0, 2]
Explanation: nums[0] + nums[2] = 3 + 1 = 8
Example 2
Input: nums = [4, 4, 2], target = 8
Output: [0, 1]
Explanation: nums[0] + nums[1] = 4 + 4 = 8
Constraints
2 <= nums.length <= 10^5-10^9 <= nums[i], target <= 10^9- Exactly one valid pair of indices exists.
Share this question
Verify Sudoku Board
Given a 9x9 Sudoku board represented as a list of 9 rows, each a list of 9 characters, determine whether the board is valid. The board may be partially filled — only the filled cells need to be checked against the standard Sudoku rules:
- Each row must contain the digits
1-9with no repeats. - Each column must contain the digits
1-9with no repeats. - Each of the nine 3x3 sub-boxes must contain the digits
1-9with no repeats.
Empty cells are represented by the character "." and are ignored by
all three rules. You do not need to check whether the board is
solvable — only whether the filled-in digits violate any rule.
Example 1
Input: board = [
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
Output: true
Example 2
Input: board where two "8"s appear in the same column (all else the
same as a valid board)
Output: false
Explanation: A column contains the digit 8 twice.
Constraints
board.length == 9board[i].length == 9board[i][j]is either a digit1-9or".".
Share this question
Zero Striping
Given an m x n integer matrix, if an element is 0, set its entire
row and column to 0. You must do this in place, modifying the
input matrix directly.
Example 1
Input:
[[1, 1, 1],
[1, 0, 1],
[1, 1, 1]]
Output:
[[1, 0, 1],
[0, 0, 0],
[1, 0, 1]]
Example 2
Input:
[[0, 1, 2, 0],
[3, 4, 5, 2],
[1, 3, 1, 5]]
Output:
[[0, 0, 0, 0],
[0, 4, 5, 0],
[0, 3, 1, 0]]
Constraints
1 <= m, n <= 200-2^31 <= matrix[i][j] <= 2^31 - 1
Share this question
Longest Chain of Consecutive Numbers
Given an unsorted array of integers nums, return the length of the
longest run of consecutive integers (i.e., integers that form a
sequence like x, x+1, x+2, ... with no gaps). The numbers do not
need to be contiguous in the original array, and duplicates should be
treated as a single value.
Your algorithm must run in O(n) time.
Example 1
Input: nums = [100, 4, 200, 1, 3, 2]
Output: 4
Explanation: The longest run is [1, 2, 3, 4].
Example 2
Input: nums = [0, 3, 7, 2, 5, 8, 4, 6, 0, 1]
Output: 9
Explanation: The longest run is [0, 1, 2, 3, 4, 5, 6, 7, 8].
Constraints
0 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9
Share this question
Geometric Sequence Triplets
Given an integer array nums and an integer ratio, count the
number of index triplets (i, j, k) such that i < j < k and
nums[j] == nums[i] * ratio and nums[k] == nums[j] * ratio (i.e.,
the three values at those indices form a geometric sequence with the
given common ratio, in left-to-right order).
Example 1
Input: nums = [1, 2, 4, 2, 4, 8], ratio = 2
Output: 3
Explanation: Valid triplets (by index): (0,1,2), (0,1,4), (0,3,4)
each satisfy nums[j] = nums[i] * 2 and nums[k] = nums[j] * 2.
Example 2
Input: nums = [1, 1, 1], ratio = 1
Output: 1
Explanation: The only triplet is (0, 1, 2), where every value is 1
and 1 * 1 = 1.
Constraints
1 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9-10^9 <= ratio <= 10^9
Share this question