Apple India | Software Engineer | December 2023 | Online Assessment
Anonymous User
4110

Recently, I gave online assessment for Apple for Software Engineer position over Hackerrank. There were total 2 coding questions and were given 50 min to complete.

Problem 1: Save the Plants

There are n number of dying plants. Each plant i has a saving factor sf[i], which is the minimum size of fertilizer bag that the plant can be saved with, and each fertilizer bag j has a size s[j]. If s[j] >= sf[i], we want to assign the fertilizer bag j to plant i, and the plant can be saved. Maximize the number of plants that we can save.

Example 1:
Input: saving factor (sf) = [1,2,3], fertilizer bag size (s) = [1,1]
Output: 1
Explanation: You have 3 plants and 2 fertilizer bags.
The saving factor of 3 plants are 1, 2, 3.
And even though you have 2 fertilizer bag, since their size is both 1, you could only save the plant having saving factor 1
You need to output 1.

Problem 2: Teacher and Gift box

You are teacher and wanted to give some gift to N students. You ordered N gift box from amazon and each gift box have some gift items inside it. But each gift box has different number of gift items inside it.

You as teacher wanted to make sure, each gift box should contains approximately same number of gifts items. And wanted to minimised the difference of gift items in any two gift box.
You have pool of infinite number of gift items with you, which you can used that pool to balance (add/subtract) gift items in any gift box but with only given two operation.

For example if you have N = 4 students and array of gift box represented as gift_boxes = [1,4,3,8], each giftboxes[i] represent number of gift items inside that gift box.

If you want, you can perform below two operation on any gift box, any number of time.

  • If number of gift items in gift box is odd then you can double the gift item in that gift box.
    • Here gift_boxes[0] has gift items=1 (odd), you can double it then your gift boxes
      array becomes = [2,4,3,8]
  • If number of gift items in gift box is even then you can half the gift item in that gift box.
    • Here gift_boxes[1] has gift items=4 (even), you can half it then your gift boxes
      array becomes = [1,2,3,8]

Now, "array-diff" of giftboxes array is maximum difference between number of gift items of any two gift box in the gift_boxes array.
Return the minimum "array-diff" of gift_boxes array can have after performing some number of above operations.

Example 1:
Input: gift_boxes = [1,4,3,8]
Output: 1
Explanation: You can transtorm the array to
[1,4,3,8] -> "array-diff" of gift_boxes array = 8-1 = 7
[2,4,3,8] -> "array-diff" of gift _boxes array = 8-2 = 6
[2,2,3,8] - "array-diff" of gift_boxes array = 8-2 = 6
[2,2,3,4] -> "array-diff" of gift_boxes array = 4-2 = 2
[2,2,3,2] - "array-diff" of gift_boxes array = 3-2 = 1
then minimum "array-diff" of gift _boxes array will be = 1.

Comments (10)