title-img


Peekable Iterator - Google Top Interview Questions

Implement an iterator of a list of integers nums where peek() returns the next element, without moving the iterator next() polls the next element in the iterator hasnext() which returns whether the next element exists Constraints n ≤ 100,000 where n is the number of calls to peek, next and hasnext Example 1 Input methods = ["constructor", "peek", "next", "hasnext", "peek", "next", "hasnext"] arguments = [[[1, 2]], [], [], [], [], [], []]` Output [None, 1, 1,

View Solution →

Quadratic Application - Google Top Interview Questions

You are given a list of integers nums sorted in ascending order, and integers a, b, and c. Apply the following function for each number x in nums: ax^2 + bx + cax 2 +bx+c and return the resulting list in ascending order. This should be done in \mathcal{O}(n)O(n) time. Constraints n ≤ 100,000 where n is the length of nums Example 1 Input nums = [-2, 3] a = 1 b = -3 c = 2 Output [2, 12] Explanation We have nums[0] = 1*-2**2 + -3*-2 + 2 = 4 + 6 + 2

View Solution →

Randomized Binary Search - Google Top Interview Questions

Consider a modified binary search, where instead of picking the middle element as the pivot, we pick it randomly between the low and the high indices. Given a list of integers nums that's not necessarily sorted, return the number of elements that will always be found using this modified binary search. Constraints n ≤ 100,000 where n is the length of nums Example 1 Input nums = [1, 10, 5, 20] Output 2 Explanation We can always find the elements 1 and 20. For the el

View Solution →

Range Query on a List - Mutable - Google Top Interview Questions

Implement a data structure with the following methods: RangeSum(int[] nums) constructs a new instance with the given nums total(int i, int j) returns the sum of integers from nums between [i, j). That is, nums[i] + nums[i + 1] + ... + nums[j - 1] update(int idx, int val) updates nums[idx] with value val Constraints n ≤ 100,000 where n is the length of nums k ≤ 100,000 where k is the number of calls to total (overflows) Example 1 Input methods = ["constructor", "tota

View Solution →

Range Update - Google Top Interview Questions

You are given a list of integers nums and a two-dimensional list of integers operations. Each operation is of the following form: [L, R, X], which means that you should increment by X all the elements from indices L to R inclusive in the list (the list is 0-indexed). Apply all operations and return the final list. Constraints n ≤ 10,000 where n is length of nums o ≤ 10,000 where o is length of operations Example 1 Input nums = [7, 3, 1, -10, 3] operations = [ [

View Solution →