Return the sum of all elements in an integer array

Quick Overview

Write a function that returns the total of every element in an integer array that can hold negative values. It tests basic array iteration, a simple loop invariant, and checking whether the result fits in a fixed-width integer type.

Return the sum of all elements in an integer array

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an integer array `nums`, return the sum of all elements in the array. ### Function Signature ```python def array_sum(nums: list[int]) -> int: ``` ### Constraints - `1 <= len(nums) <= 10^4` - `-10^4 <= nums[i] <= 10^4` ### Examples ```text Input: nums = [1, 2, 3, 4, 5] Output: 15 ``` 1 + 2 + 3 + 4 + 5 = 15. ```text Input: nums = [-1, -2, -3, 4, 6] Output: 4 ``` The negative values add up to -6 and the positive values add up to 10, so the total is 4. ```text Input: nums = [-7] Output: -7 ``` With a single element, the sum is that element.

Overview: Write a function that returns the total of every element in an integer array that can hold negative values. It tests basic array iteration, a simple loop invariant, and checking whether the result fits in a fixed-width integer type.

Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array nums, return the sum of all elements in the array.

Function Signature

def array_sum(nums: list[int]) -> int:

Constraints

  • 1 <= len(nums) <= 10^4
  • -10^4 <= nums[i] <= 10^4

Examples

Input: nums = [1, 2, 3, 4, 5]
Output: 15

1 + 2 + 3 + 4 + 5 = 15.

Input: nums = [-1, -2, -3, 4, 6]
Output: 4

The negative values add up to -6 and the positive values add up to 10, so the total is 4.

Input: nums = [-7]
Output: -7

With a single element, the sum is that element.