Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Federico Madden
My Pythonic solution

Federico Madden

Jun 30, 2025

My solution using pythonic contructs like aggregation functions and array slicing. Here we assume slicing uses no extra space, though it may in practice

class Solution: def findMaxSumSubArray(self,k, arr): # return 0 if k=0 if k == 0: return 0 if k == 1: return max(arr) # init max sum to 0 max_sum = 0 # first iteration # init current sum to sum(arr[0:k]) (python array slicing is end-exclusive) curr_sum = sum(arr[0:k]) # update max sum max_sum = max(curr_sum, max_sum) # loop where we iterate over start (0) and end (k) elts, both increment by 1 after each iteration # We need to gracefully handle the case where k=n for start, end in zip(arr, arr[k:]): # update the current sum by subtracting the start elt and adding the end elt curr_sum -= start curr_sum += end # update max sum max_sum = max(curr_sum, max_sum) return max_sum

0

0

Comments
Comments

On this page