PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of array algorithms and competency in identifying and reasoning about minimum and maximum values within contiguous subarrays, emphasizing extremal value handling.

  • medium
  • TikTok
  • Coding & Algorithms
  • Software Engineer

Maximize min+max of contiguous subarray

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given an array of **n** positive integers `nums`. Find a **contiguous subarray** that contains **at least two elements** and maximizes the value: > (minimum element of the subarray) + (maximum element of the subarray) Return this maximum possible sum. If multiple subarrays yield the same maximum sum, you only need to return the value of that sum (not the subarray itself). You may assume `n ≥ 2`. --- **Example** Input: ```text nums = [5, 12, 9, 6, 4] ``` All valid subarrays of length ≥ 2 and their `(min + max)` values include: - `[5, 12]` → min = 5, max = 12, sum = 17 - `[5, 12, 9]` → min = 5, max = 12, sum = 17 - `[12, 9]` → min = 9, max = 12, sum = 21 - `[12, 9, 6]` → min = 6, max = 12, sum = 18 - `[9, 6]` → min = 6, max = 9, sum = 15 - ... The maximum possible value of `min + max` is `21`, from subarray `[12, 9]`. **Output:** ```text 21 ```

Quick Answer: This question evaluates understanding of array algorithms and competency in identifying and reasoning about minimum and maximum values within contiguous subarrays, emphasizing extremal value handling.

You are given an array of positive integers nums. Find a contiguous subarray that contains at least two elements and maximizes the value: (minimum element of the subarray) + (maximum element of the subarray). Return this maximum possible sum. If multiple subarrays yield the same maximum sum, return only the value.

Constraints

  • 2 <= len(nums) <= 200000
  • 1 <= nums[i] <= 1000000000

Examples

Input: ([5, 12, 9, 6, 4],)

Expected Output: 21

Explanation: The subarray [12, 9] has min 9 and max 12, giving 21, which is the maximum possible value.

Input: ([3, 8],)

Expected Output: 11

Explanation: There is only one valid subarray, [3, 8], so the answer is 3 + 8 = 11.

Input: ([1, 2, 3, 4, 5],)

Expected Output: 9

Explanation: The best subarray is [4, 5], giving min 4 plus max 5 = 9.

Input: ([10, 1, 10],)

Expected Output: 11

Explanation: The full subarray [10, 1, 10] gives 11, but adjacent subarrays [10, 1] and [1, 10] also give 11.

Input: ([7, 7, 7, 7],)

Expected Output: 14

Explanation: Every valid subarray has min 7 and max 7, so the maximum sum is 14.

Input: ([1000000000, 1, 999999999],)

Expected Output: 1000000001

Explanation: The best adjacent sums are 1000000000 + 1 and 1 + 999999999, so the maximum is 1000000001.

Hints

  1. For any valid subarray, look at where its maximum element is. Since the subarray has at least two elements, that maximum has at least one neighbor inside the subarray.
  2. Can every longer subarray's min + max be matched or exceeded by some adjacent pair inside it?
Last updated: Jun 25, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Parse a nested list from a string - TikTok (medium)
  • Implement stacks, streaming median, and upward path sum - TikTok (easy)
  • Implement stack variants and path-sum check - TikTok (medium)
  • Solve common string/DP/stack problems - TikTok (medium)
  • Find the longest palindromic substring - TikTok (easy)