Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Add and Print Array Output

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Add and Print Array Output. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

ABC Family needs a small utility to calculate totals from an array of integer values. Given an array, traverse it once, add every element, and return the final sum so the caller can print it.

Formal Specification

Implement sum_array(nums), where nums is a list of integers. Return a single integer equal to the sum of all values in nums. The function should not modify the input array. For an empty array, return 0.

The caller may print the returned value as needed. Do not use Python's built-in sum() function, so the implementation demonstrates basic array traversal and accumulation.

Constraints

  • 0 <= nums.length <= 10^4
  • -10^6 <= nums[i] <= 10^6
  • Return the sum as an integer
  • Do not modify the input array

Function Signature

def sum_array(nums):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output