Quick Overview

This question evaluates a candidate's understanding of array-based data processing, difference maximization, and basic algorithmic optimization for time-series inputs. It is commonly asked in Coding & Algorithms interviews to assess fundamental practical programming ability and attention to edge cases (e.g.

Compute max profit from one stock trade

Company: Flipster

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

Given an integer array `prices` where `prices[i]` is the stock price on day `i`, compute the **maximum profit** you can achieve by choosing **at most one** day to buy and a later day to sell. - You may complete **0 or 1** transaction. - You must buy before you sell. ### Input - `prices`: array of integers, `n >= 1` ### Output - Maximum possible profit (integer). ### Example - Input: `[7, 1, 5, 3, 6, 4]` - Output: `5` (buy at 1, sell at 6) ### Edge cases - Prices always decreasing → return `0`.

Overview: This question evaluates a candidate's understanding of array-based data processing, difference maximization, and basic algorithmic optimization for time-series inputs. It is commonly asked in Coding & Algorithms interviews to assess fundamental practical programming ability and attention to edge cases (e.g.

Return the maximum profit from at most one buy followed by one later sell. Return 0 if no profitable trade exists.

Constraints

  • n >= 1 in normal inputs

Examples

Input: ([7, 1, 5, 3, 6, 4],)

Expected Output: 5

Input: ([7, 6, 4, 3, 1],)

Expected Output: 0

Hints

  1. Track the minimum price seen so far.

Max Profit from One Stock Trade

easyFlipsterSoftware Engineer

Return the maximum profit from at most one buy followed by one later sell. Return 0 if no profitable trade exists.

View full question (all parts)

Given an integer array prices where prices[i] is the stock price on day i, compute the maximum profit you can achieve by choosing at most one day to buy and a later day to sell.

  • You may complete 0 or 1 transaction.
  • You must buy before you sell.

Input

  • prices: array of integers, n >= 1

Output

  • Maximum possible profit (integer).

Example

  • Input: [7, 1, 5, 3, 6, 4]
  • Output: 5 (buy at 1, sell at 6)

Edge cases

  • Prices always decreasing → return 0.

Examples

Example 1
Input
([7, 1, 5, 3, 6, 4],)
Output
5
Example 2
Input
([7, 6, 4, 3, 1],)
Output
0

Constraints

  • •n >= 1 in normal inputs

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Case 1
Input
([7, 1, 5, 3, 6, 4],)
Expected
5
Case 2
Input
([7, 6, 4, 3, 1],)
Expected
0

Browse More Questions