Skip to content

Instantly share code, notes, and snippets.

@johntran
Last active October 23, 2018 19:30
Show Gist options
  • Save johntran/e66eb956b223dac7249c8aa6b46b2a29 to your computer and use it in GitHub Desktop.
Save johntran/e66eb956b223dac7249c8aa6b46b2a29 to your computer and use it in GitHub Desktop.

Whiteboarding 2018 Oct 23

typing

Problem Set

Find the best times to buy and sell given an array of stock prices

The cost of a stock on each day is given in an array, find the single highest profit that you can make by buying and selling in those days. For example, if the given array is {100, 180, 260, 310, 40, 535, 695}, the maximum profit can earned by buying on day 4 and sell on day 6. It would return 694-40. If the given array of prices is sorted in decreasing order, return the lowest loss.

Merge Overlapping Intervals

Given a set of time intervals in any order, merge all overlapping intervals into one and output the result which should have only mutually exclusive intervals. Let the intervals be represented as pairs of integers for simplicity. For example, let the given set of intervals be {{1,3}, {2,4}, {5,7}, {6,8} }. The intervals {1,3} and {2,4} overlap with each other, so they should be merged and become {1, 4}. Similarly {5, 7} and {6, 8} should be merged and become {5, 8}

Area under histogram

Find the largest rectangular area possible in a given histogram where the largest rectangle can be made of a number of contiguous bars. For simplicity, assume that all bars have same width and the width is 1 unit.

For example, consider the following histogram with 7 bars of heights {6, 2, 5, 4, 5, 1, 6}. The largest possible rectangle possible is 12 (see the below figure, the max area rectangle is highlighted in red)

histogram

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment