4. ReLU
relu()Asked in screens shaped like: Google, Meta
Implement relu(x): return the element-wise maximum of x and 0. Return a list of floats.
Examples
Example 1
Input: { "x": [-2, 0, 3] } Output: [0, 0, 3]
Constraints
- 1 <= n <= 10**5, where n is the length of the input
- Input is non-empty
Implement relu(). Types below are the NumPy contract; Python and PyTorch use lists or tensors with the same names.
| Name | Type | I/O |
|---|---|---|
| x | array | Input |
| value | array | Output |
Same tests, three APIs. Pick a language with the chips above the editor.
NumPy
Vectorized arrays. Default for analyst, scientist, and DE screens.
Revealed one at a time. The reference implementation stays in the Solution tab.
Hint 1
Keep the function signature. Fill the body — do not rename parameters.
Hint 2
Match the rounding in the examples. Tests compare with a small numeric tolerance.
Hint 3
Switch NumPy / Python / PyTorch with the chips above the editor. Each language has its own tests.
Run your code to see stdout.
Submit to run the checks.
All tests passed on this language.
Next problem: Dot product →