FastPrepHandwritten Sigmoid
Problem · Math

Handwritten Sigmoid

Learn this problem
EasyAmazon logoAmazonINTERNPHONE SCREEN
See Amazon hiring insights

Problem statement

Given a finite double x, return its logistic sigmoid:

sigmoid(x) = 1 / (1 + exp(-x))

Implement the computation directly with standard scalar arithmetic and the standard exponential function. Do not call a machine-learning or numerical-array library.

Function

sigmoid(x: double) → double

Examples

Example 1

x = 0.0return = 0.5

At x = 0, exp(0) = 1, so the result is 1 / 2.

Example 2

x = 2.0return = 0.8807970779778823

Substituting 2 into the logistic formula gives approximately 0.8807970779778823.

Example 3

x = -2.0return = 0.11920292202211755

The logistic values at opposite inputs sum to 1, so this is 1 - sigmoid(2).

Constraints

  • -1000 <= x <= 1000
  • x is finite: it is not NaN or infinity.
  • The result is compared with absolute tolerance 1e-12.

More Amazon problems

drafts saved locally
public double sigmoid(double x) {
    // write your code here
}
x0.0
expected0.5
checking account