META | Random pick index & Subarray sum K

Anonymous User
1084
Nov 07, 2023
Nov 07, 2023

Q1. Variation of https://leetcode.com/problems/random-pick-index/ Given stream of integers which can have duplicates. return random index of largest number
eg: 1, 1, 9, 2, 3, 9, 9 probabilty of 9 to return is 1/3

Solution: used reservoir sampling.
time - o(n)
space- o(1)


Q2: Subarray Sum Equals K Given list of non-negative integers and an integer target, return whether a continuous sequence of input integers sums up to target.
// Test case 1
int[] numbers1 = {1, 2, 3, 4, 5};
int target1 = 9;
return true

// Test case 2
int[] numbers2 = {1, 3, 2, 5, 7, 2};
int target2 = 14;
return true

// Test case 3
int[] numbers3 = {4, 3, 2, 7, 1, 2};
int target3 = 10;
return true

// Test case 4
int[] numbers4 = {4, 3, 2, 7, 1, 2};
int target4 = 11;
return false

Solution: used sliding window apprach.
time - o(n)
space - o(1)

is there any better solution than this ? can anyone help.

Comments (7)