PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches

Quick Overview

This question evaluates understanding of numerical stability, floating-point arithmetic, normalization, and probability distributions when computing softmax outputs from real-valued inputs.

  • medium
  • XPeng
  • Coding & Algorithms
  • Software Engineer

Implement Stable Softmax

Company: XPeng

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Implement a function that computes the normalized exponential values of a list of real numbers. Given an array `x` of length `n`, return an array `p` of length `n` where: `p[i] = exp(x[i]) / sum(exp(x[j]) for j = 0..n-1)` Requirements: - The returned values should sum to approximately `1.0`. - Handle very large or very small input values without numerical overflow or underflow where possible. - If the input array is empty, return an empty array. Example: Input: `[1.0, 2.0, 3.0]` Output: approximately `[0.0900, 0.2447, 0.6652]`

Quick Answer: This question evaluates understanding of numerical stability, floating-point arithmetic, normalization, and probability distributions when computing softmax outputs from real-valued inputs.

Implement a function that computes the softmax of a list of real numbers. Given an array x of length n, return an array p of length n where p[i] = exp(x[i]) / sum(exp(x[j]) for j = 0..n-1). The result should be computed in a numerically stable way so that very large or very small values do not cause unnecessary overflow or underflow. If the input array is empty, return an empty array.

Constraints

  • 0 <= n <= 100000
  • -1000000000 <= x[i] <= 1000000000
  • Each x[i] is a finite real number

Examples

Input: ([1.0, 2.0, 3.0],)

Expected Output: [0.09003057317038046, 0.24472847105479764, 0.6652409557748218]

Explanation: This is the standard softmax of [1, 2, 3]. The largest value gets the highest probability.

Input: ([],)

Expected Output: []

Explanation: An empty input should return an empty list.

Input: ([1000.0, 1001.0, 1002.0],)

Expected Output: [0.09003057317038046, 0.24472847105479764, 0.6652409557748218]

Explanation: A stable implementation subtracts the maximum value first, so this produces the same distribution as [1, 2, 3] without overflow.

Input: ([2.0, 2.0, 2.0],)

Expected Output: [0.3333333333333333, 0.3333333333333333, 0.3333333333333333]

Explanation: All inputs are equal, so each position gets equal probability.

Input: ([0.0],)

Expected Output: [1.0]

Explanation: With only one element, its probability must be 1.0.

Hints

  1. Softmax does not change if you subtract the same constant from every element.
  2. Try subtracting the maximum value in the list before applying exp.
Last updated: May 23, 2026

Related Coding Questions

  • Build a Least-Recently-Used Store - XPeng (medium)
  • Compute optimal matrix multiplication order - XPeng (Medium)

Loading coding console...

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.