
Goldman Sachs

20+ Goldman Sachs Software Analyst Interview Questions and Answers
Q1. Word Search Problem Statement
Given a two-dimensional grid of size N x M
consisting of upper case characters and a string 'WORD', determine how many times the 'WORD' appears in the grid.
The 'WORD' can be forme...read more
Count the number of occurrences of a given word in a 2D grid by moving in all eight possible directions.
Iterate through each cell in the grid and start searching for the word from that cell in all eight directions.
Keep track of the number of occurrences found while searching.
Handle edge cases like word length exceeding grid dimensions or starting from out-of-bounds cells.
Q2. Fenwick Tree Problem Statement
You are provided with an array/list ARR
consisting of 'N' integers, along with 'Q' queries. Each query can be of one of the following two types:
- Type 1 (Range Sum): Given two int...read more
The problem involves executing range sum and point update queries on an array using Fenwick Tree data structure.
Use Fenwick Tree to efficiently handle range sum and point update queries.
For range sum queries, calculate prefix sums and use them to compute the sum in the given range.
For point update queries, update the Fenwick Tree accordingly to reflect the changes in the array.
Ensure to handle the queries efficiently to meet the time constraints.
Example: Given array [3, 2, 1,...read more
Q3. Climbing the Leaderboard Problem Statement
In a game leaderboard, scores determine rankings where the highest score gets rank 1. Players with identical scores share the same rank, followed by the next rank down...read more
The task is to determine a player's leaderboard rank for each game score based on given leaderboard and game scores.
Iterate through each game score and compare it with leaderboard scores to determine the rank.
Use a hashmap to store the leaderboard scores and their corresponding ranks.
Handle cases where multiple players have the same score and share the same rank.
Return an array of ranks for each game score.
Q4. Flatten the Multi-Level Linked List Problem
You are provided with a multi-level linked list consisting of 'N' nodes. Each node contains a 'next' and 'child' pointer that may point to another node. Your task is ...read more
The task is to flatten a multi-level linked list into a singly linked list and return the head of the modified linked list.
Traverse the linked list and keep track of nodes with child pointers
Flatten the child linked list and merge it with the main linked list
Update the 'next' pointers to create a singly linked list
Return the head of the modified linked list
Q5. Design a HashSet
Create a HashSet data structure without using any built-in hash table libraries.
Functions Description:
1) Constructor: Initializes the data members as required. 2) add(value): Inserts an eleme...read more
Design a HashSet data structure with add, contains, and remove functions.
Implement a HashSet using an array and handling collisions using chaining or open addressing.
Use a hash function to map values to indices in the array.
For add function, insert the value at the hashed index.
For contains function, check if the value exists at the hashed index.
For remove function, delete the value at the hashed index and return it, or return -1 if not found.
Q6. Partition Set Into Two Subsets With Minimum Difference
Given an array of N non-negative integers, split the array into two subsets such that the absolute difference between their sums is minimized.
The task is ...read more
Split array into two subsets with minimum absolute difference between their sums.
Use dynamic programming to find the minimum absolute difference between the sums of two subsets.
Iterate through all possible sums of subsets and find the minimum difference.
Consider each element either being included in the first subset or the second subset.
Keep track of the minimum absolute difference found so far.
Return the minimum absolute difference between the sums of the two subsets.
Q7. Print Nodes at Distance K from a Given Node
Given an arbitrary binary tree, a node of the tree, and an integer 'K', find all nodes that are at a distance K from the specified node, and return a list of these no...read more
Given a binary tree, a target node, and an integer K, find all nodes at distance K from the target node.
Traverse the binary tree to find the target node.
Use BFS to traverse the tree from the target node to nodes at distance K.
Keep track of the distance while traversing the tree.
Return the values of nodes at distance K in any order.
Q8. Partition to K Equal Sum Subsets Problem
Given an array of integers and a positive integer 'K', determine if it is possible to divide the array into 'K' non-empty subsets such that the sum of elements in each s...read more
The problem involves dividing an array into K subsets with equal sum.
Use backtracking to try all possible combinations of dividing the array into K subsets.
Keep track of the sum of elements in each subset and check if they are equal to the target sum.
Optimize the backtracking algorithm by pruning branches that cannot lead to a valid solution.
Consider edge cases such as empty array, negative numbers, and unequal division of elements.
Ensure that the sum of all elements in the a...read more
Q9. Find Pairs in a Doubly-Linked List
A sorted doubly-linked list of distinct positive integers is provided, along with an integer 'X'. Your task is to identify and print all unique pairs from the list whose sum e...read more
Find pairs in a sorted doubly-linked list whose sum equals a given integer 'X'.
Traverse the list from both ends to find pairs with sum equal to 'X'.
Use two pointers approach to efficiently find the pairs.
Handle cases where the sum is less than, equal to, or greater than 'X'.
Q10. Maximum Subarray Sum Problem Statement
Given an array arr
of length N
consisting of integers, find the sum of the subarray (including empty subarray) with the maximum sum among all subarrays.
Explanation:
A sub...read more
Find the sum of the subarray with the maximum sum among all subarrays in an array of integers.
Iterate through the array and keep track of the maximum sum subarray seen so far.
At each index, decide whether to include the current element in the subarray or start a new subarray.
Update the maximum sum subarray if a new maximum is found.
Consider edge cases like all negative numbers in the array.
Example: For input N=5, arr=[-2, 1, -3, 4, -1], the output is 4.
Q11. Two and Four Wheeler Roads Problem Statement
There is a country with N
cities and M
bidirectional roads of 3 types:
Type 1: Two Wheeler Road - Only vehicles with two wheels can use this road. Type 2: Four Wheel...read more
Determine the maximum number of roads that can be removed while ensuring a path exists for every pair of cities for two-wheeler and four-wheeler vehicles.
Identify the type of road (two-wheeler, four-wheeler, or both) and its impact on the connectivity between cities.
Consider the constraints provided in the input to optimize the solution.
Remove roads strategically to maximize the number of roads removed while maintaining connectivity.
If not all cities can be reached, return -1...read more
Q12. Problem Statement: Minimize the Maximum
You are given an array of integers and an integer K
. For each array element, you can adjust it by increasing or decreasing it by a value of K
. Your goal is to minimize th...read more
The goal is to minimize the difference between the maximum and minimum elements of an array by adjusting each element by a given value.
Iterate through each test case and calculate the minimum possible difference between the maximum and minimum elements after modifications.
For each element in the array, consider increasing or decreasing it by the given value K to minimize the overall difference.
Return the calculated minimum difference for each test case.
Q13. Next Greater Number Problem Statement
Given a string S
which represents a number, determine the smallest number strictly greater than the original number composed of the same digits. Each digit's frequency from...read more
The task is to find the smallest number greater than the given number, using the same set of digits.
Iterate from right to left to find the first digit that can be swapped with a smaller digit on its right.
Swap this digit with the smallest digit on its right that is greater than it.
Sort the digits to the right of the swapped digit in ascending order to get the smallest number.
If no such digit is found, return -1.
Example: For '56789', swap '8' with '9' to get '56798'.
Q14. Rat in a Maze Problem Statement
You need to determine all possible paths for a rat starting at position (0, 0) in a square maze to reach its destination at (N-1, N-1). The maze is represented as an N*N matrix w...read more
The task is to find all possible paths for a rat to navigate through a maze from start to finish.
Use backtracking algorithm to explore all possible paths in the maze.
Keep track of visited cells to avoid infinite loops.
Generate paths by moving in all possible directions (up, down, left, right) until reaching the destination.
Return the list of valid paths sorted in alphabetical order.
Q15. Minimum Number of Platforms Needed Problem Statement
You are given the arrival and departure times of N trains at a railway station for a particular day. Your task is to determine the minimum number of platform...read more
The minimum number of platforms needed at a railway station to avoid train waiting times is determined based on arrival and departure times.
Sort the arrival and departure times in ascending order.
Iterate through the times and keep track of the number of platforms needed at any given time.
Update the minimum number of platforms needed as you iterate through the times.
Return the minimum number of platforms needed.
Q16. Prime Numbers Identification
Given a positive integer N
, your task is to identify all prime numbers less than or equal to N
.
Explanation:
A prime number is a natural number greater than 1 that has no positive d...read more
Identify all prime numbers less than or equal to a given positive integer N.
Iterate from 2 to N and check if each number is prime
Use the Sieve of Eratosthenes algorithm for efficient prime number identification
Optimize by only checking up to the square root of N for divisors
Q17. Maximize Stock Trading Profit
You are given an array prices
, representing stock prices over N consecutive days. Your goal is to compute the maximum profit achievable by performing multiple transactions (i.e., b...read more
To maximize stock trading profit, find maximum profit achievable by buying and selling shares multiple times.
Iterate through the array of stock prices and find all increasing sequences of prices.
Calculate profit by buying at the start of each increasing sequence and selling at the end.
Sum up all profits to get the maximum profit achievable.
Q18. Minimum Depth of a Binary Tree Problem Statement
Given a Binary Tree of integers, determine the minimum depth of the Binary Tree. The minimum depth is defined as the number of nodes present along the shortest p...read more
The minimum depth of a Binary Tree is the number of nodes along the shortest path from the root node to the nearest leaf node.
Traverse the Binary Tree using BFS (Breadth First Search) to find the minimum depth
Keep track of the level of each node while traversing
Stop the traversal as soon as a leaf node is encountered and return the level as the minimum depth
Q19. Arithmetic Progression Queries Problem Statement
Given an integer array ARR
of size N
, perform the following operations:
- update(l, r, val):
Add (val + i)
to arr[l + i]
for all 0 ≤ i ≤ r - l
.
- rangeSum(l, r):...read more
The problem involves updating and calculating the sum of elements in an array based on given operations.
Implement update(l, r, val) to add (val + i) to arr[l + i] for all i in range [0, r - l].
Implement rangeSum(l, r) to return the sum of elements in the array from index l to r.
Handle queries using 1-based indexing and output the sum of arr[l..r] for each rangeSum operation.
Q20. Find K-th Smallest Element in BST
Given a binary search tree (BST) and an integer K
, the task is to find the K-th smallest element in the BST.
Example:
Input:
BST: Order of elements in increasing order is { 2, ...read more
To find the K-th smallest element in a BST, perform an in-order traversal and return the K-th element encountered.
Perform in-order traversal of the BST to get elements in increasing order
Keep track of the count of elements visited and return the K-th element
Time complexity can be optimized by maintaining a count of nodes in each subtree
A website similar to Instagram for travelers, allowing users to share photos and stories from their trips.
Include features like geotagging to show where photos were taken
Allow users to create travel itineraries and share tips with others
Implement a rating system for destinations and accommodations
Enable users to connect with fellow travelers and plan trips together
Q22. Implement two stacks in a single array
Implement two stacks in a single array using two different approaches
Divide the array into two halves and use one half for each stack
Use two pointers, one for each stack, and move them towards each other as elements are pushed or popped
Consider edge cases like stack overflow and underflow
Q23. Difference between SQL and NoSQL
SQL is a traditional relational database management system, while NoSQL is a non-relational database system.
SQL is structured, uses tables with predefined schema, and is good for complex queries.
NoSQL is unstructured, uses collections or documents, and is good for large amounts of data with simple queries.
SQL is ACID compliant, ensuring data integrity, while NoSQL is BASE (Basically Available, Soft state, Eventually consistent) for scalability and flexibility.
Examples of SQL ...read more
Q24. Reverse a linked list recursively
Reverse a linked list recursively by swapping pointers
Start by checking if the current node is null or the next node is null
If so, return the current node as it is the new head of the reversed list
Otherwise, recursively call the function on the next node and swap pointers
Q25. Implement a min stack
A min stack is a stack data structure that supports the usual push and pop operations, along with an additional operation to retrieve the minimum element in constant time.
Create a stack to store the elements and another stack to store the minimum values encountered so far.
When pushing an element, check if it is smaller than the current minimum. If so, push it onto the minimum stack.
When popping an element, check if it is equal to the current minimum. If so, pop from the minim...read more
More about working at Goldman Sachs

Interview Process at Goldman Sachs Software Analyst

Top Software Analyst Interview Questions from Similar Companies





Reviews
Interviews
Salaries
Users/Month

