Now that i am officially a fulltime software engineer, i thought it was about time that i start diving deeper into data structures and algorithms.
I did a quick search on google and multiple sources pointed toward something known as the blind 75 .
This list was curated by a facebook tech lead to help all of the "time-constrained engineers" out there looking for a job.
These questions teach you the core concepts and techniques for each category/type of problem. many of the leetcode type interview questions that you will most definitely run into when trying to break into "big tech" are either directly chosen from this list or a combination/mashup of a few of them.
two sum
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function (nums, target) {
//set up results object
results = {};
for (let i = 0; i < nums.length; i++) {
// check to see if complement exists
let complement = target - nums[i];
if (results.hasOwnProperty(complement)) {
return [results[complement], i];
}
results[nums[i]] = i;
}
return null;
};
The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).
best time to buy and sell stock
You are given an array prices where prices[i] is the price of a given stock on the ith day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
// My initial naive solution timed out.
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function (prices) {
let buyPrice = 0;
let sellPrice = 0;
let profit = 0;
for (let i = 0; i < prices.length; i++) {
buyPrice = prices[i];
for (let j = i + 1; j < prices.length; j++) {
if (prices[j] > buyPrice) {
sellPrice = prices[j];
if (sellPrice - buyPrice > profit) {
profit = sellPrice - buyPrice;
}
}
}
}
return profit;
};
// After checking the discussion, I came to this solution.
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function (prices) {
// trying sliding window
let buy = 0;
let sell = 1;
let profit = 0;
for (let i = 0; i < prices.length - 1; i++) {
if (prices[sell] - prices[buy] > profit) {
profit = prices[sell] - prices[buy];
}
if (prices[buy] > prices[sell]) {
buy = sell;
}
sell = sell + 1;
}
return profit;
};
The condition to use the sliding window technique is that the problem asks to find the maximum (or minimum) value for a function that calculates the answer repeatedly for a set of ranges from an array.
contains duplicate
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function (nums) {
let results = {};
for (let i = 0; i < nums.length; i++) {
if (results.hasOwnProperty(nums[i])) {
return true;
}
results[nums[i]] = i;
}
return false;
};
Nothing really to note here... This was an easy one.
product of array except self
Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in O(n) time and without using the division operation.
// insert solution here
find minimum in rotated sorted array
// insert solution here
Number of Connected Components in an Undirected Graph
// insert solution here
Remove Nth Node From End of List
// insert solution here
Longest Substring Without Repeating Characters
// insert solution here
Longest Repeating Character Replacement
// insert solution here
Binary Tree Level Order Traversal
// insert solution here
Serialize and Deserialize Binary Tree
// insert solution here
Construct Binary Tree from Preorder and Inorder Traversal
// insert solution here
- Sliding Window
- Two Pointers or Iterators
- Fast and Slow Pointers
- Merge Intervals
- Cyclic Sort
- In-Place Reversal of - Linked List
- Tree BFS
- Tree DFS
- Two Heaps
- Subsets
- Modified Binary Search
- Top K Elements
- K-Way Merge
- Topological Sort