title-img


Trie - Amazon Top Interview Questions

Implement a trie data structure with the following methods: Trie() constructs a new instance of a trie add(String word) adds a word into the trie exists(String word) returns whether word exists in the trie startswith(String p) returns whether there's some word whose prefix is p Constraints n ≤ 100,000 where n is the number of calls to add, exists and startswith Example 1 Input methods = ["constructor", "add", "add", "exists", "startswith", "exists"] arguments = [[], ["dog"],

View Solution →

Longest Consecutive Sequence - Amazon Top Interview Questions

Given an unsorted array of integers nums, find the length of the longest sequence of consecutive elements. Constraints n ≤ 100,000 where n is the length of nums Example 1 Input nums = [100, 4, 200, 1, 3, 2] Output 4 Explanation The longest sequence of consecutive elements is [1, 2, 3, 4]. so we return its length: 4. Example 2 Input nums = [100, 4, 200, 1, 3, 2, 101, 105, 103, 102, 104] Output 6 Explanation The longest sequence of consecutive elem

View Solution →

Look and Say - Amazon Top Interview Questions

The "look and say" sequence is defined as follows: beginning with the term 1, each subsequent term visually describes the digits appearing in the previous term. The first few terms are as follows: 1 11 <- 1 one 21 <- 2 ones 1211 <- 1 two, 1 one 111221 <- 1 one, 1 two, 2 ones 312211 <- 3 ones, 2 twos, 1 one Given an integer n, return the nth term of this sequence as a string. Constraints 1 ≤ n ≤ 40 Example 1 Input n = 3

View Solution →

File System - Amazon Top Interview Questions

Implement a data structure with the following methods: bool create(String path, int content) which creates a path at path if there's a parent directory and path doesn't exist yet, and sets its value as content. Returns whether it was newly created. Initially only the root directory at "/" exists. int get(String path) which returns the value associated with path. If there's no path, return -1. Constraints 0 ≤ n ≤ 100,000 where n is the number of calls to create and get Example 1 Inp

View Solution →

Wildfire - Amazon Top Interview Questions

You are given a two-dimensional integer matrix representing a forest where a cell is: 0 if it's empty 1 if it's a tree 2 if it's a tree on fire Every day, a tree catches fire if there is an adjacent (top, down, left, right) tree that's also on fire. Return the number of days it would take for every tree to be on fire. If it's not possible, return -1. Constraints 0 ≤ n * m ≤ 200,000 where n and m are the number of rows and columns in matrix Example 1 Input matrix = [ [1,

View Solution →