Chicago Trading OA

Task 1

There are two wooden sticks of lengths A and B respectively. Each stick can be cut into multiple shorter sticks of integer lengths. Your goal is to construct the largest possible square. To do this, you must cut four sticks of the same length (note that there can be some leftover pieces). What is the longest side of the square that you can achieve?

Find and fix a bug in the given implementation of a function:

int solution(int A, int B);

that, given two integers A and B, returns the side length of the largest square that you can obtain. If it is not possible to create any square, the function should return 0.

Examples:

  1. Given A = 10, B = 21, the function should return 7. You can split the second stick into three sticks of length 7 and shorten the first stick by 3.
  2. Given A = 13, B = 11, the function should return 5. You can cut two sticks of length 5 from each of the given sticks.

The attached code is still incorrect for some inputs. Despite the error(s), the code may produce a correct answer for the example test cases. The goal of the exercise is to find and fix the bug(s) in the implementation. You can modify at most one line.

Constraints:

  • A and B are integers within the range [1..100,000].

In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.


Task 2

You are given N cards. Each card consists of a suit and a rank. There are four suits, S (Spades), H (Hearts), D (Diamonds), and C (Clubs), and thirteen ranks, ordered (from the lowest to the highest) 2, 3, 4, 5, 6, 7, 8, 9, 10, J (Jack), Q (Queen), K (King), and A (Ace).

Each card is represented by a string in the format <rank><suit>. For example, 10 of Spades is described as "10S", and Queen of Hearts as "QH".

There are six ranked card sets (described in detail below). Sets are ordered by their strength from the weakest to the strongest. Your task is to detect the card sets that can be formed from the given cards. Detecting each card set is scored separately and is worth an equal number of points. If you detect more than one set, select the strongest one.

Write a function:

Results solution(vector<string> &cards);

that, given an array of strings cards, returns a Results object representing the strongest card set that can be formed.

Assume that the following declarations are given:

struct Results {  
    string set_name;  
    vector<string> selected_cards;  
};
  • set_name is a string which should be the name of the detected set (see below).
  • selected_cards is a list of strings, which should be made of cards that form the detected set. Cards can be listed in any order.

Scoring:

  • Detecting each card set is scored separately and is worth an equal number of points.
  • In tests worth 50% of points, only the strongest set's set_name is checked.

Card sets:

Set 1: single card

  • Name: "single card"
  • Description: A single card of the highest rank; the suit does not matter.
  • Example: cards = ["2H", "4H", "7C", "9D", "10D", "KS"] Output: { "set_name": "single card", "selected_cards": ["KS"] }

Set 2: pair

  • Name: "pair"
  • Description: Two cards sharing the same rank; suits do not matter. If there are multiple pairs, return any one with the highest rank.
  • Example: cards = ["AS", "10H", "8H", "10S", "8D"] Output: { "set_name": "pair", "selected_cards": ["10H", "10S"] }

Set 3: triple

  • Name: "triple"
  • Description: Three cards sharing the same rank; suits do not matter.
  • Example: cards = ["AS", "JS", "AH", "AD", "10D", "AC"] Output: { "set_name": "triple", "selected_cards": ["AH", "AD", "AC"] }

Set 4: five in a row

  • Name: "five in a row"
  • Description: Five cards of consecutive ranks starting with the highest possible rank; suits do not matter.
  • Example: cards = ["6H", "7S", "8S", "9S", "10S", "JD", "JC", "KC", "AC"] Output: { "set_name": "five in a row", "selected_cards": ["7S", "8S", "9S", "10S", "JC"] }

Set 5: suit

  • Name: "suit"
  • Description: Five cards sharing the same suit; the ranks do not matter.
  • Example: cards = ["2D", "4D", "6D", "8D", "9D", "AC", "KC", "QC", "JC", "7C"] Output: { "set_name": "suit", "selected_cards": ["2D", "4D", "6D", "8D", "9D"] }

Set 6: a triple and a pair

  • Name: "a triple and a pair"
  • Description: Five cards, consisting of a triple (three cards of the same rank) and a pair (two cards of the same rank).
  • Example: cards = ["10D", "10H", "10C", "2S", "2H", "2D", "JH", "JC"] Output: { "set_name": "a triple and a pair", "selected_cards": ["10D", "10H", "10C", "JH", "JC"] }

Constraints:

  • N is an integer within the range [1, 10].
  • The elements of cards are all distinct.
  • Each string in cards is a correct representation of a card in <rank><suit> format.

In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.


Task 3

You are given a record of the historical prices of an investment asset from the last N days. Analyze the record in order to calculate what could have been your maximum income. Assume you started with one asset of this type and could hold at most one at a time. You could choose to sell the asset whenever you held one. If you did not hold an asset at some moment, you could always afford to buy an asset (assume you had infinite money available).

What is the maximum income you could make?

Write a function:

int solution(vector<int> &A);

that, given an array A of length N representing a record of prices over the last N days, returns the maximum income you could make. As the result may be large, return its last nine digits without leading zeros (return the result modulo 1,000,000,000).

Examples:

  1. Given A = [4, 1, 2, 3], the function should return 6. You could sell the product on the first day (for 4), buy it on the second (for 1), and sell it again on the last day (for 3). The income would be equal 4 - 1 + 3 = 6.
  2. Given A = [1, 2, 3, 3, 2, 1, 5], the function should return 7. You could sell the product when its value was 3, buy it when it changed to 1, and sell it again when it was worth 5.
  3. Given A = [1000000000, 1, 2, 2, 1000000000, 1, 1000000000], the function should return 999999998. The maximum possible income is 2999999998, whose last 9 digits are 999999998.

Constraints:

  • N is an integer within the range [1..200,000].
  • Each element of array A is an integer within the range [0..1,000,000,000].

Write an efficient algorithm for the following assumptions.


我们长期稳定承接各大科技公司如TikTok、Google、Amazon等的OA笔试代写服务,确保满分通过。如有需求,请随时联系我们。

We consistently provide professional online assessment services for major tech companies like TikTok, Google, and Amazon, guaranteeing perfect scores. Feel free to contact us if you're interested.

Leave a Reply

Your email address will not be published. Required fields are marked *