title-img


Justify Text - Amazon Top Interview Questions

Given a list of words and an integer line length k, return a list of strings which represents each line fully justified, with as many words as possible in each line. There should be at least one space between each word, and pad extra spaces when necessary so that each line has exactly length k. Spaces should be distributed as equally as possible, with any extra spaces distributed starting from the left. If you can only fit one word on a line, then pad the right-hand side with spaces. Ea

View Solution →

Largest Binary Search Subtree in Nodes - Amazon Top Interview Questions

Given a binary tree root, find the largest subtree (the one with the most nodes) that is a binary search tree. Constraints n ≤ 100,000 where n is the number of nodes in root Example 1 Input root = [1, [3, [2, null, null], [5, null, null]], null] Output [3, [2, null, null], [5, null, null]] Explanation The root is not a valid binary search tree, but the tree beginning at 3 is.

View Solution →

Largest Binary Search Subtree in Value - Amazon Top Interview Questions

Given a binary tree root, return the largest sum of a subtree that is also a binary search tree. Constraints 0 ≤ n ≤ 100,000 where n is the number of nodes in root Example 1 Input root = [1, [5, [6, null, null], [7, null, null]], [3, [2, null, null], [4, null, null]]] Output 9 Explanation The binary search tree rooted at 3 has the largest sum with 3 + 2 + 4 = 9

View Solution →

Largest Rectangle Submatrix - Amazon Top Interview Questions

Given a two-dimensional integer matrix consisting only of 1s and 0s, return the area of the largest rectangle containing only 1s. Constraints 0 ≤ n, m ≤ 250 where n and m are the number of rows and columns in matrix Example 1 Input matrix = [ [1, 0, 0, 0], [1, 0, 1, 1], [1, 0, 1, 1], [0, 1, 0, 0] ] Output 4 Explanation The biggest rectangle here is the 2 by 2 square of 1s on the right. Example 2 Input matrix = [ [1, 0, 0, 0, 0],

View Solution →

Least Frequently Used Cache - Amazon Top Interview Questions

Implement a least frequently used cache with the following methods: LFUCache(int capacity) constructs a new instance of a LFU cache with the given capacity. get(int key) retrieves the value associated with the key key. If it does not exist, return -1. As a side effect, this key's usage is incremented (used for eviction). set(int key, int val) updates the key key with value val. If updating this key-value pair exceeds capacity, then evicts the least frequently used key-value pair. If

View Solution →