Gradient Descent on a Sinusoidal Objective
Updated
Problem statement
Use gradient descent to look for a minimum of f(x) = x*x - 2*x + 1 + 3*sin(pi*x). Here pi is π, and all sine and cosine inputs are in radians.
Start at x = initialX. Make exactly iterations updates, each with the same learningRate. Each update uses the derivative (slope) of f at the current x:
x = x - learningRate * (2*x - 2 + 3*pi*cos(pi*x)).
After the last update, return [x, f(x)]. If there are zero iterations, return the starting x and its f value. Use double-precision numbers. Each returned number is accepted with an absolute or relative error of up to 1e-6. You may also estimate the derivative numerically, as long as it is accurate enough.
In this exercise, the limits on the start and the learning rate keep every update in a stable region where f is convex, meaning it curves upward like a bowl. Because you make a fixed number of updates, the result is an estimate. Do the requested updates. Do not swap in a different optimizer or hardcode the minimum point.
Function
gradientDescent(initialX: double, learningRate: double, iterations: int) → double[]Examples
Example 1
initialX = 1.5learningRate = 0.01iterations = 0return = [1.5,-2.75]With zero iterations nothing changes. x stays 1.5, and f(x) is -2.75.
Example 2
initialX = 1.5learningRate = 0.01iterations = 1return = [1.49,-2.7584196810971946]The derivative at the start is 1, so one update moves x to 1.49. Plug this new x into the original f to get the second number.
Example 3
initialX = 1.5learningRate = 0.01iterations = 100return = [1.4683142604203359,-2.7658306044951817]After 100 updates, x has moved a little further downhill. The result is only an estimate after that many steps. It is not a promise that this is the lowest point overall.
Constraints
1.0 <= initialX <= 2.0.0.000001 <= learningRate <= 0.03.0 <= iterations <= 10000.- The inputs are finite numbers. All trigonometric calculations use radians.
More Headlands Technologies Phone Screen questions
- Integer Compression OffsetPHONE SCREEN · Seen Oct 2026
- Market Order BookPHONE SCREEN · Seen Jun 2026
- Pearson Correlation CoefficientPHONE SCREEN · Seen Apr 2026
- Top K Frequent ElementsPHONE SCREEN · Seen Apr 2026
- Queue Mean from Corrective SnapshotsPHONE SCREEN · Seen Mar 2026
- Queue Mean QueriesPHONE SCREEN · Seen Mar 2026
- CPU Register Bitwise OperationsPHONE SCREEN · Seen Nov 2025
- Merge K Sorted SequencesPHONE SCREEN · Seen Oct 2024
- Top Trade Events by NotionalPHONE SCREEN · Seen May 2024
- Fixed-Size Memory PoolPHONE SCREEN · Seen May 2024
Similar questions at other companies
- Sliding Window Attention over a KV CacheNVIDIA · PHONE SCREENNVIDIA interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Monthly Calendar with Custom Week StartStripe · PHONE SCREENStripe interview questions
- Fractional Share Inventory FlatteningRobinhood · PHONE SCREEN · ONSITE INTERVIEWRobinhood interview questions
- Logistic Regression DiagnosticsLinkedIn · PHONE SCREENLinkedIn interview questions
- Evenly Schedule Vehicle InspectionsAmazon · PHONE SCREEN
- Implement Cross-Entropy LossOpenAI · PHONE SCREENOpenAI interview questions
- Cumulative No-Intercept Regression BetasCitadel · PHONE SCREENCitadel interview questions
- Add Signed Character ArraysByteDance · PHONE SCREENByteDance interview questions
- Per-Segment SpeedingOnePay · PHONE SCREENOnePay interview questions