Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Max Sum of Non-Adjacent Elements

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

Your question is Max Sum of Non-Adjacent Elements. 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

A Quora Feed ranking experiment assigns an integer score to each candidate item. Given an array of scores, choose a subset of items with no two selected items adjacent, maximizing the total score. You may choose no items, so the result is 0 when every score is negative.

Formal Specification

Implement a function that accepts nums, a list of integers, and returns an integer representing the maximum possible sum of non-adjacent elements. Adjacent means consecutive positions in the original array. You do not need to return the selected indices.

Constraints

  • 1 <= nums.length <= 100,000
  • -10^9 <= nums[i] <= 10^9
  • The result fits within a signed 64-bit integer
  • Selecting no elements is allowed

Function Signature

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