PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Goldman Sachs

Complete decision tree and gradient descent functions

Last updated: Mar 29, 2026

Quick Overview

This question evaluates implementation skills in supervised machine learning and numerical optimization, specifically computing Gini impurity and selecting optimal binary splits for a decision tree alongside deriving and applying a single-step gradient descent update for linear regression.

  • hard
  • Goldman Sachs
  • Coding & Algorithms
  • Machine Learning Engineer

Complete decision tree and gradient descent functions

Company: Goldman Sachs

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Take-home Project

You are given partially implemented code and must complete key functions. Implement the missing parts with correct logic and reasonable efficiency. ## Task A) Decision tree split function (classification) You are building a binary decision tree classifier for tabular numeric features. Implement: 1. `gini(y)` that computes the Gini impurity of labels `y` (binary labels 0/1). 2. `best_split(X, y)` that finds the best `(feature_index, threshold)` to split on. Definitions/requirements: - `X` is an `n x d` matrix of floats. - `y` is length `n`, values in `{0,1}`. - A split is defined by `(j, t)` meaning: - left child: samples with `X[i][j] <= t` - right child: samples with `X[i][j] > t` - Choose the split that **minimizes weighted Gini impurity**: \[ G_{split} = \frac{n_L}{n} G(y_L) + \frac{n_R}{n} G(y_R) \] - If a split would produce an empty side, it is invalid. - Return `None` if no valid split exists. Constraints: - `1 <= n <= 2e4`, `1 <= d <= 50` - Aim for ~\(O(nd \log n)\) or better. ## Task B) Single-step gradient descent update (linear regression) You are fitting linear regression with mean squared error: \[ L(w,b) = \frac{1}{n}\sum_{i=1}^n (w^\top x_i + b - y_i)^2 \] Implement: - `gd_step(X, y, w, b, lr)` that performs **one** gradient descent update and returns `(new_w, new_b)`. Requirements: - `X` is `n x d`, `y` length `n` (real-valued). - `w` length `d`, `b` scalar. - Use the correct gradients for MSE (include the averaging over `n`). - Must work for `n=1` and `d=1`. ## Notes - You may assume Python-like pseudocode and basic numeric operations (no need for full training loop). - Handle edge cases cleanly (constant labels, identical feature values, etc.).

Quick Answer: This question evaluates implementation skills in supervised machine learning and numerical optimization, specifically computing Gini impurity and selecting optimal binary splits for a decision tree alongside deriving and applying a single-step gradient descent update for linear regression.

Related Interview Questions

  • Implement an Integer Hash Map - Goldman Sachs
  • Solve string and hashmap coding tasks - Goldman Sachs (medium)
  • Find first non-repeating character index - Goldman Sachs (nan)
  • Solve string and hashmap interview tasks - Goldman Sachs (medium)
  • Count segments and optimize 3-server assignment - Goldman Sachs (medium)
Goldman Sachs logo
Goldman Sachs
Oct 10, 2025, 12:00 AM
Machine Learning Engineer
Take-home Project
Coding & Algorithms
5
0

You are given partially implemented code and must complete key functions. Implement the missing parts with correct logic and reasonable efficiency.

Task A) Decision tree split function (classification)

You are building a binary decision tree classifier for tabular numeric features.

Implement:

  1. gini(y) that computes the Gini impurity of labels y (binary labels 0/1).
  2. best_split(X, y) that finds the best (feature_index, threshold) to split on.

Definitions/requirements:

  • X is an n x d matrix of floats.
  • y is length n , values in {0,1} .
  • A split is defined by (j, t) meaning:
    • left child: samples with X[i][j] <= t
    • right child: samples with X[i][j] > t
  • Choose the split that minimizes weighted Gini impurity :

Gsplit=nLnG(yL)+nRnG(yR)G_{split} = \frac{n_L}{n} G(y_L) + \frac{n_R}{n} G(y_R)Gsplit​=nnL​​G(yL​)+nnR​​G(yR​)

  • If a split would produce an empty side, it is invalid.
  • Return None if no valid split exists.

Constraints:

  • 1 <= n <= 2e4 , 1 <= d <= 50
  • Aim for ~ O(ndlog⁡n)O(nd \log n)O(ndlogn) or better.

Task B) Single-step gradient descent update (linear regression)

You are fitting linear regression with mean squared error:

L(w,b)=1n∑i=1n(w⊤xi+b−yi)2L(w,b) = \frac{1}{n}\sum_{i=1}^n (w^\top x_i + b - y_i)^2L(w,b)=n1​∑i=1n​(w⊤xi​+b−yi​)2

Implement:

  • gd_step(X, y, w, b, lr) that performs one gradient descent update and returns (new_w, new_b) .

Requirements:

  • X is n x d , y length n (real-valued).
  • w length d , b scalar.
  • Use the correct gradients for MSE (include the averaging over n ).
  • Must work for n=1 and d=1 .

Notes

  • You may assume Python-like pseudocode and basic numeric operations (no need for full training loop).
  • Handle edge cases cleanly (constant labels, identical feature values, etc.).

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Goldman Sachs•More Machine Learning Engineer•Goldman Sachs Machine Learning Engineer•Goldman Sachs Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.