Skip to main content

Goldman Sachs Data Structures & Algorithms Interview Questions

Review this list of 15 Goldman Sachs Data Structures & Algorithms Data Scientist interview questions and answers verified by hiring managers and candidates.
  • Goldman Sachs logoAsked at Goldman Sachs 
    +38

    "public static boolean isPalindrome(String str){ boolean flag = true; int len = str.length()-1; int j = len; for(int i=0;i<=len/2;i++){ if(str.charAt(i)!=str.charAt(j--)){ flag = false; break; } } return flag; }"

    Sravanthi M. - "public static boolean isPalindrome(String str){ boolean flag = true; int len = str.length()-1; int j = len; for(int i=0;i<=len/2;i++){ if(str.charAt(i)!=str.charAt(j--)){ flag = false; break; } } return flag; }"See full answer

    Data Scientist
    Data Structures & Algorithms
    +4 more
  • Goldman Sachs logoAsked at Goldman Sachs 
    Video answer for 'Find the container with the maximum volume of water.'

    "int getMaxWater(vector& nums) { int n = nums.size(); int mx = INT_MIN; int i=0, j=n-1; while(i<j) { int water = (j - i) * min(nums[i], nums[j]); mx = max(mx, water); if(nums[i] < nums[j]){ i++; } else { j--; } } return mx; } `"

    Richard W. - "int getMaxWater(vector& nums) { int n = nums.size(); int mx = INT_MIN; int i=0, j=n-1; while(i<j) { int water = (j - i) * min(nums[i], nums[j]); mx = max(mx, water); if(nums[i] < nums[j]){ i++; } else { j--; } } return mx; } `"See full answer

    Data Scientist
    Data Structures & Algorithms
    +3 more
  • Goldman Sachs logoAsked at Goldman Sachs 
    +24

    "We can use dictionary to store cache items so that our read / write operations will be O(1). Each time we read or update an existing record, we have to ensure the item is moved to the back of the cache. This will allow us to evict the first item in the cache whenever the cache is full and we need to add new records also making our eviction O(1) Instead of normal dictionary, we will use ordered dictionary to store cache items. This will allow us to efficiently move items to back of the cache a"

    Alfred O. - "We can use dictionary to store cache items so that our read / write operations will be O(1). Each time we read or update an existing record, we have to ensure the item is moved to the back of the cache. This will allow us to evict the first item in the cache whenever the cache is full and we need to add new records also making our eviction O(1) Instead of normal dictionary, we will use ordered dictionary to store cache items. This will allow us to efficiently move items to back of the cache a"See full answer

    Data Scientist
    Data Structures & Algorithms
    +6 more
  • +1

    " Compare alternate houses i.e for each house starting from the third, calculate the maximum money that can be stolen up to that house by choosing between: Skipping the current house and taking the maximum money stolen up to the previous house. Robbing the current house and adding its value to the maximum money stolen up to the house two steps back. package main import ( "fmt" ) // rob function calculates the maximum money a robber can steal func maxRob(nums []int) int { ln"

    VContaineers - " Compare alternate houses i.e for each house starting from the third, calculate the maximum money that can be stolen up to that house by choosing between: Skipping the current house and taking the maximum money stolen up to the previous house. Robbing the current house and adding its value to the maximum money stolen up to the house two steps back. package main import ( "fmt" ) // rob function calculates the maximum money a robber can steal func maxRob(nums []int) int { ln"See full answer

    Data Scientist
    Data Structures & Algorithms
    +4 more
  • +11

    " def hasgoodsubarray(nums, k): if not nums: return False if k == 0: for i in range(len(nums)): if nums[i] == 0 and nums[i + 1] == 0: return True return False map = {0:-1} sum = 0 for i,val in enumerate(nums): sum += val rem = sum % k if rem in map: if i - map[rem] >= 2: return True else: map[rem] = i return False print(hasgoods"

    Abinash S. - " def hasgoodsubarray(nums, k): if not nums: return False if k == 0: for i in range(len(nums)): if nums[i] == 0 and nums[i + 1] == 0: return True return False map = {0:-1} sum = 0 for i,val in enumerate(nums): sum += val rem = sum % k if rem in map: if i - map[rem] >= 2: return True else: map[rem] = i return False print(hasgoods"See full answer

    Data Scientist
    Data Structures & Algorithms
    +4 more
  • 🧠 Want an expert answer to a question? Saving questions lets us know what content to make next.

  • Goldman Sachs logoAsked at Goldman Sachs 

    "Use a representative of each, e.g. sort the string and add it to the value of a hashmap> where we put all the words that belong to the same anagram together."

    Gaston B. - "Use a representative of each, e.g. sort the string and add it to the value of a hashmap> where we put all the words that belong to the same anagram together."See full answer

    Data Scientist
    Data Structures & Algorithms
    +4 more
  • Goldman Sachs logoAsked at Goldman Sachs 
    +20

    " function isValid(s) { const openBrackets = new Set(['(','{','[']); const closeBrackets = new Map([ [')', '('], ['}', '{'], [']', '['] ]); const stack = []; for (let i = 0; i < s.length; i++) { if (openBrackets.has(s[i])) { stack.push(s[i]); } else { if (closeBrackets.get(s[i]) !== stack.pop()) { return false; } } } return stack.length === 0; } // deb"

    Jeff S. - " function isValid(s) { const openBrackets = new Set(['(','{','[']); const closeBrackets = new Map([ [')', '('], ['}', '{'], [']', '['] ]); const stack = []; for (let i = 0; i < s.length; i++) { if (openBrackets.has(s[i])) { stack.push(s[i]); } else { if (closeBrackets.get(s[i]) !== stack.pop()) { return false; } } } return stack.length === 0; } // deb"See full answer

    Data Scientist
    Data Structures & Algorithms
    +4 more
  • Goldman Sachs logoAsked at Goldman Sachs 
    +9

    " function climbStairs(n) { // 4 iterations of Dynamic Programming solutions: // Step 1: Recursive: // if (n <= 2) return n // return climbStairs(n-1) + climbStairs(n-2) // Step 2: Top-down Memoization // const memo = {0:0, 1:1, 2:2} // function f(x) { // if (x in memo) return memo[x] // memo[x] = f(x-1) + f(x-2) // return memo[x] // } // return f(n) // Step 3: Bottom-up Tabulation // const tab = [0,1,2] // f"

    Matthew K. - " function climbStairs(n) { // 4 iterations of Dynamic Programming solutions: // Step 1: Recursive: // if (n <= 2) return n // return climbStairs(n-1) + climbStairs(n-2) // Step 2: Top-down Memoization // const memo = {0:0, 1:1, 2:2} // function f(x) { // if (x in memo) return memo[x] // memo[x] = f(x-1) + f(x-2) // return memo[x] // } // return f(n) // Step 3: Bottom-up Tabulation // const tab = [0,1,2] // f"See full answer

    Data Scientist
    Data Structures & Algorithms
    +3 more
  • Goldman Sachs logoAsked at Goldman Sachs 
    +47

    "function twoSum(nums, target) { const n = nums.length const map = new Map() for (let i=0; i<n; i++) { if (map.has(nums[i])) return [map.get(nums[i]), i] const diff = target - nums[i] map.set(diff, i) } return [] } `"

    Maciej Z. - "function twoSum(nums, target) { const n = nums.length const map = new Map() for (let i=0; i<n; i++) { if (map.has(nums[i])) return [map.get(nums[i]), i] const diff = target - nums[i] map.set(diff, i) } return [] } `"See full answer

    Data Scientist
    Data Structures & Algorithms
    +5 more
  • Goldman Sachs logoAsked at Goldman Sachs 
    Video answer for 'Find the median of two sorted arrays.'
    Data Scientist
    Data Structures & Algorithms
    +4 more
  • Goldman Sachs logoAsked at Goldman Sachs 
    +10

    "static boolean hasCycle(ListNode head) { if (head == null || head.next == null) { return false; } ListNode slow = head; ListNode fast = head.next; while (slow != null && fast.next != null) { if (slow == fast) { return true; } slow = slow.next; fast = fast.next.next; } return false; } "

    Laksitha R. - "static boolean hasCycle(ListNode head) { if (head == null || head.next == null) { return false; } ListNode slow = head; ListNode fast = head.next; while (slow != null && fast.next != null) { if (slow == fast) { return true; } slow = slow.next; fast = fast.next.next; } return false; } "See full answer

    Data Scientist
    Data Structures & Algorithms
    +4 more
  • Data Scientist
    Data Structures & Algorithms
    +4 more
  • Goldman Sachs logoAsked at Goldman Sachs 
    Video answer for 'Given the root of a binary tree of integers, return the maximum path sum.'

    "\# Definition for a binary tree node. class TreeNode: def init(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right class Solution: def maxPathSum(self, root: TreeNode) -> int: self.max_sum = float('-inf')"

    Jerry O. - "\# Definition for a binary tree node. class TreeNode: def init(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right class Solution: def maxPathSum(self, root: TreeNode) -> int: self.max_sum = float('-inf')"See full answer

    Data Scientist
    Data Structures & Algorithms
    +4 more
  • Goldman Sachs logoAsked at Goldman Sachs 
    Data Scientist
    Data Structures & Algorithms
    +4 more
  • Goldman Sachs logoAsked at Goldman Sachs 
    +9

    " import java.io.*; import java.util.*; class Solution { static int trapRainWater(int[] height) { // your code goes here if(height.length == 0) { return 0; } int n = height.length; int[] left = new int[n]; int[] right = new int[n]; Arrays.fill(left, Integer.MAX_VALUE); Arrays.fill(right, Integer.MAX_VALUE); left[0] = height[0]; right[n-1] = height[n-1]; for(int i = 1; i < n - 1;"

    Basil A. - " import java.io.*; import java.util.*; class Solution { static int trapRainWater(int[] height) { // your code goes here if(height.length == 0) { return 0; } int n = height.length; int[] left = new int[n]; int[] right = new int[n]; Arrays.fill(left, Integer.MAX_VALUE); Arrays.fill(right, Integer.MAX_VALUE); left[0] = height[0]; right[n-1] = height[n-1]; for(int i = 1; i < n - 1;"See full answer

    Data Scientist
    Data Structures & Algorithms
    +4 more
Showing 1-15 of 15