Goldman Sachs

https://medium.com/@hch.hkcontact/goldman-sachs-top-50-leetcode-questions-q7-high-five-a933247c219a
 

Coderpad

  • boolean isPowerOfTen (int x)
    • 循环%10,看最后的结果是不是1
    • 或者利用换底公式 - 通解通发,log(3)8 = log(10)8  /log(10)3 
    • 判断结果是不是整数,用%1 == 0
    • // Need to handle case where x > INTEGER.MAX_VALUE or < MIN_VALUE  
  • int findMin(int a[]):
    • 二分法,
    • 结束条件:left - right = 1,两个指针相邻的时候
    • 特别的:当 循环到left = mid = right, 需要改用遍历的方式寻找[left, right]段的最小值
    • // array is sorted in increasing order, but is rotated some spaces i.e. {6, 1, 2, 3, 4, 5} or {4, 5, 6, 1, 2, 3}
      // must be completed in O(log n) time  

Interview Session

 describe how to implement HashMap and how to implement ArrayList
 
Round 1: Coder pad -> Simple High Five question with some tweaks and some LR questions.
Round 2: Code pad -> Hash Map question using Generics. Now, this was tricky as you have to code real time.

Round 3, Super Day : This had three rounds 45 mins each without a break
1: Round 1 : They asked me about SQL, basic queries of Unique and also to get the largest element. 2 Sum, 3 Sum, 4 Sum problem.
2: Round 2: SDLC principles : Now, this was mostly a summary of my experience and how one will get to production design, unit testing and overall working of the whole software. I felt it was positive, but I guess I failed in answering some of the questions for database related unit testing. Mochito was the answer.
3: Round 3: System design round, The interviewer shared a document which had a futures problem and how I would reduce the total transactions over the system to save company money. Plus some leadership principles and one question about my approach in using React and Angular JS.

Received a mail from HR that I was not selected.

[Goldman Sachs Top 50 LeetCode Questions] Q7. High Five

Andrew H., Front Office Developer

Andrew H., Front Office Developer

Feb 25, 2020·2 min read

 

If you are preparing a coding interview for GS, this series surely helps you. I have coded the most optimized solutions of 50 LeetCode questions tagged with Goldman Sachs.

 

Goal:

  • get average of top 5 scores of each student & arrange in order of each id

Question:

  • how to get top 5 scores for each student?
  • how to arrange scores information in order of id?

how to get the top 5 scores for each student?

There are some choices of data structure in getting sorted data

First, use a list, then use the sorting.

  • Time complexity = O(n log n), n = total no. of scores of each student

Second, use a priority queue to store top k elements

  • Time complexity = O(n log k), n = total no. of scores of each student, k=no. of top of scores you need, which is 5 here

Third, use an array to have buckets to store scores

  • Time complexity = O(n+m), n = total no. of scores of each student, m=total no. of possible scores, which is 100 here

Across them, the third choice has the fastest performance, which is linear speed.

how to arrange scores information in order of id?

There are some choices of store data in order

First, the list is not good.

  • As if you want to put the score to be together with the stored scores based on the id, you have to search the index using O(k) time complexity, k is no. of students
  • In total, the time complexity is O(n k), n is no.of items

Second, TreeMap can be a choice.

  • adding need O(log k) time complexity, k is the size of the map, which means no. of students
  • In total, as you have to add n items, the time complexity is O(n log k), n is no. of items.

Third, an array can be a choice.

  • searching and then need is O(1) time complexity as the id of student equal the index of the array
  • In total, the time complexity is O(n), n is no. of items

Across them, the third choice has the fastest performance, which is linear speed.

Steps:

First, use a 2d matrix to store the count of scores of each student.

  • e.g. matrix[5][80] =3 means the student of id 5 get 3 times of 80 scores
  • e.g. matrix[10][70] =0 means the student of id 10 get 0 times of 70 scores

Second, iterate the items array to fill up the matrix

Third, iterate the across students in the 2d matrix

  • iterate all the score bucket from 100 to 0 to find the top 5 scores
  • calculate the average to put it in result list

Performance:

Time complexity:

  • filling matrix: O(k), k is no. of items
  • getting average: O(m*n), m is no. of students, n is no. of scores
  • total: base on the requirement, the time complexity mainly determined by O(m*n)

Space complexity:

  • O(m*n), m is no. of students, n is no. of scores

Java Code Example:

WRITTEN BY

Andrew H., Front Office Developer

love competitive programming, algorithm, machine learning

 

 
 
 
 
 
 

 
 
 
 

love competitive programming, algorithm, machine learning

 
 

Feb 24, 2020

 

If you are preparing a coding interview for GS, this series surely helps you. I have coded the most optimized solutions of 50 LeetCode questions tagged with Goldman Sachs.

 

Goal:

  • to find out the probability that the knight is still on the board after n iterations

Question:

  1. if given 10x10 board, what is the probability that the knight is still on board after the 1st iteration?
  2. Next, what is the probability that the knight is still on board after the 2nd iteration?

if given 10x10 board, what is the probability that the knight is still on board after the 1st iteration?

Let’s assume the starting point is at (0,0). There is a probability of 0.125 to go to (2,1) and the same probability to go to (1,2) to keep it still on the board. All other directions give a 0 probability. The probability is still on board = 0.125*2= …

Read more · 2 min read

 

 
 

 
 

Feb 24, 2020

 

If you are preparing a coding interview for GS, this series surely helps you. I have coded the most optimized solutions of 50 LeetCode questions tagged with Goldman Sachs.

 

Remove Comments - LeetCode

Given a C++ program, remove comments from it. The program source is an array where source[i] is the i-th line of the…

leetcode.com

 

Goal:

it is to get the filtered strings, stored in a list of string

Questions:

First, to decide whether you add char into current String

Second, to decide whether you add current String to result list

Whether you add char into current String?

Most of the time we add current char into String, but in below conditions, we do not add it.

  1. if in scope but it is one of the signs (“/*”, “//”)
  2. if NOT in scope (met “/*” sign before in this or previous lines without closing it…

Read more · 2 min read

 

 
 

 
 

Feb 23, 2020

 

If you are preparing a coding interview for GS, this series surely helps you. I have coded the most optimized solutions of 50 LeetCode questions tagged with Goldman Sachs.

 

Game of Life - LeetCode

According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by…

leetcode.com

 

Inspiration Question:

Image for post
Image for post
  1. Imagine a 3x3 matrix.

Rule 1: Only live cells with 2 or 3 live neighbors can live. Otherwise, dead.

Rule 2: Only dead cells with exactly 3 neighbors can live. Otherwise, dead.

what is the result?

(1 means live, 0 mean dead)

Read more · 4 min read

 

 

 
 

Feb 23, 2020

 

If you are preparing a coding interview for GS, this series surely helps you. I have coded the most optimized solutions of 50 LeetCode questions tagged with Goldman Sachs.

 

Climbing Stairs - LeetCode

You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In…

leetcode.com

 

How many ways to get to level=i?

  1. Get to level 1
Image for post
Image for post
  • Obviously, only 1 way
  • from level 0 to level 1
 

2. Get to level 2

Read more · 3 min read

 

 
 

 
 

Feb 22, 2020

 

If you are preparing a coding interview for GS, this series surely helps you. I have coded the most optimized solutions of 50 LeetCode questions tagged with Goldman Sachs.

 

Minimum Size Subarray Sum - LeetCode

Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of…

leetcode.com

 

Brute Force approach

e.g. with an array of [2,1,5,6,3,11], find minimum length of the subarray, which sum≥10

Using brute force, enumerate all possible subarray of the array.

  • i=0: [2], [2,1], [2,1,5], [2,1,5,6], [2,1,5,6,3], [2,1,5,6,3,11]
  • i=1: [1], [1,5], [1,5,6], [1,5,6,3], [1,5,6,3,11]
  • i=2: [5], [5,6], [5,6,3], [5,6,3,11]
  • i=3: [6], [6,3], [6,3,11]
  • i=4: [3], [3,11]
  • i=5: [11]

The above bold subarray both have sum≥10. Record the minimum length of these subarrays.

 

Observing horizontally, whenever you find a match with sum≥10, you can stop search possible answer using this index i=0 . e.g. …

Read more · 2 min read

 
 
 
原文地址:https://www.cnblogs.com/frankcui/p/14299885.html