After telephone screen by Recuriter I chose to give an attempt at SDE II. I been in IT for 17 years and still wanted to do a SDE II so I can align myself with Amazon standards. Prepared for about 4 weeks. First Coding Interview book started with Leet but it was out of order then I chose AlgoMonster and completed all topic used by Amazon. Prepared 2 examples for each LP. Attended yesterday .
Following is C# code I have written. I chose to fill in 100 Items array with Probablities for each Index and picked a random Item. Which seemed correct. Word random and percentage was conflicting may be I messed up may be I said Sorry I am nervous that interviewer didnt like. So many questions. Either way. You all give enough time and do all the problems. Manytimes and dont take the prep easily . You got to invest yourself this is no easy task. Good Luck youall
public class Solution {
private int[] items;
private int totalSum;
private int[] prefixSums;
public Solution(int[] w)
{
this.prefixSums = new int[w.Length];
this.items = w;
int prefixSum = 0;
for (int i = 0; i < w.Length; ++i)
{
prefixSum += w[i];
this.prefixSums[i] = prefixSum;
}
this.totalSum = prefixSum;
}
public int PickIndex()
{
int[] randomPicks = new int[100];
int counter = 0;
for (int i = 0; i < items.Length; i++)
{
var count = ((double)items[i] / (double)this.totalSum) * 100;
for (int j = counter; j < counter + count; j++)
{
randomPicks[j] = i;
}
counter = counter + (int)count;
}
Random r=new Random();
return randomPicks[r.Next(0, 99)];
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(w);
* int param_1 = obj.PickIndex();
*/