Coding problems Flashcards
Find the Maximum Product of Two Integers in an Array
[Arrays]
Iterate through the array and keep track of the maximum and second maximum, as well as the minimum and second minimum numbers. The maximum product will be either max1 * max2 or min1 * min2.
Rotate an Array (k steps)
[Arrays]
There are various ways to do this, but one method is to reverse the entire array, then reverse the first k elements, and finally reverse the last n-k elements, where k is the number of rotations and n is the length of the array.
Maximum Subarray Sum - “Kadane’s Algorithm”
[Arrays]
Initialize two variables, max_current and max_global. Iterate through the array, updating max_current to be the maximum of the current element and the sum of max_current and the current element. Update max_global whenever max_current is greater than max_global.
Remove Duplicates from Sorted Array
[Arrays]
Use two pointers. One pointer iterates through the array, while the other points to the location where the next unique element should go. Swap elements as necessary.
Move All Zeros to the End
[Arrays]
Use a pointer to keep track of the last non-zero element’s index. Iterate through the array and swap the current non-zero element with the zero after last non-zero element.
Two Sum - one array and target, return indices of the two numbers such that they add up to target.
[Arrays]
Use a hash map to store visited numbers and their indices. For each element, check if (target - element) exists in the hash map.
Find the “Kth” Max/Min Element of an Array
[Arrays]
Use QuickSelect, a variation of the QuickSort algorithm, to find the Kth smallest/largest element in expected O(n) time.
Merge Two Sorted Arrays
[Arrays]
Use two pointers, one for each array. Compare the current elements pointed by the two pointers and put the smaller one into the result array, then move the pointer of the smaller element.
Intersection of Two Arrays
[Arrays]
Use two sets to hold the unique elements of each array. Take the intersection of the sets.
Longest Consecutive Sequence
[Arrays]
Put all elements in a hash set for quick look-up. Then, iterate through the array. For each number n, first check if this is the first number of subsequence (by checking for n - 1), and then look for consecutive numbers n+1, n+2, … in the set, keeping track of the longest sequence.
Find the Majority Element
Given an array of integers, find the element that appears more than n/2 times.
(Boyer-Moore Voting Algorithm)
Initialize a candidate and a counter. Iterate through the array, updating the counter if the element is the same +1 or not -1. If the counter goes below 0, then change the candidate.
Verify the candidate by counting its occurrences in a second pass.
Product of Array Except Self
Compute an output array such that output[i] is equal to the product of all elements in the array except arr[i]
First, traverse the array from left-to-right, storing the product of all numbers to the left of each index. Do a second right-to-left traversal to calculate the product of all numbers to the right, and then multiply the two.
Search in Rotated Sorted Array
An array is sorted and then rotated. Find a target value in it.
Use binary search with added conditions to identify which half of the array is still sorted after the rotation. Depending on the sorted half, decide whether to adjust low or high pointers.
Three Sum
Find all unique triplets in the array that sum up to a specific target.
First, sort the array. Then, for each element, use a two-pointer technique to find pairs that sum up to the negation of the current element.
Spiral Order Matrix
Given a matrix, return all elements in spiral order.
Keep track of boundaries (top, bottom, left, right), and iterate through the matrix updating these boundaries as you collect elements in spiral order.