Logic Triple Kill

Question Type: logic

Question (Easy)

Far away in cold winter near a big smiling tree, a confusing child was told a hard truth about his birthright. He just recently learned that his father is not who he grew up with. To verify this claim, he gathers information and he knows that blood type can be used to determine whether someone is possible to be the child of another person.

Here’s this list of possible blood type of the child based on parent:

parentparentchild
AAA, O
ABA, B, O, AB
AOA, O
AABA, B, AB
BBB, O
BOB, O
BABA, B, AB
OOO
OABA, B
ABABA, B, AB

The first 2 lines are blood types of their parents. The last input is their blood type.

Example InputExample Output
A, B, ABtrue
A, O, Bfalse

Driver Code

function isMatch(parent1, parent2, child) {
  // TODO: Do the magic here
}

isMatch("A", "B", "AB"); // true
isMatch("A", "O", "B"); // false

Question (Medium)

Universal Studios is one of the most famous amusement parks in the world. It is often crowded with many visitors queueing at each attraction. The queueing system at Universal is not a normal queue (First in First Out), but a priority queue based on ticket price (higher-priced ticket means higher priority). Universal Studios sell 3 types of tickets (sorted from the most expensive): Express Unlimited, Express, and Regular One-Day. In the queue line, there are X Express Unlimited holders, Y Express holders, and Z Regular One-Day holders.

The queue goes as follows:

  1. N Express Unlimited holders exit the queue (if there is nobody left, skip this step)
  2. N Express holders exit the queue (if there is nobody left, skip this step)
  3. N Regular One-Day holders exit the queue (if there is nobody left, skip this step)
  4. Repeat from step 1, until there is nobody left

A visitor is bored because he has been waiting at the queueing line for a while, and he is trying to distract himself by observing this queueing system and trying to answer a simple question:

What type of ticket that visitor number Q (who exit the queue) holds?

Function Description

Complete the function findTicketType in the editor below. The function should return an integer value which indicates the ticket type. Here are the list of all possible integer values and their correspondence ticket type:

findTicketType has 5 parameters, the same 5 variables which are indicated at the Input Format below:

Input Format

  • The first line contains an integer X, which indicates the number of Express Unlimited holders
  • The second line contains an integer Y, which indicates the number of Express holders
  • The third line contains an integer Z, which indicates the number of Regular One-Day holders
  • The fourth line contains an integer N, which indicates the total number of visitors who can exit the queue at one time
  • The fifth line contains an integer Q, which indicates visitor number in the question

Constraints

  • 1 <= X, Y, Z <= 1000
  • 1 <= N <= 50
  • 1 <= Q <= (X+Y+Z)

Output Format

Print the integer value which indicates the type of ticket that visitor number Q (who exit the queue) holds. As stated above, the options are:

Integer Ticket Type

Integer ValueTicket Type
0Express Unlimited
1Express
2Regular One-Day

Sample (1)

Sample Input:

  • 5
  • 10
  • 15
  • 1
  • 2

Sample Output:

  • 1

Explanation:

  • Visitor number 1 who holds Express Unlimited exit the queue, 4 Express Unlimited Holders left
  • Visitor number 2 who holds Express exit the queue, 9 Express holders left

Sample (2)

Sample Input:

1
2
10
3
5

Sample Output:

2

Explanation:

  • Visitor number 1 who holds the Express Unlimited exit the queue, no Express Unlimited holders left
  • Visitor number 2 and 3 who holds Express exit the queue. No Express holders left
  • Visitor number 4, 5, 6 who holds Regular One-Day exit the queue. 7 Regular One-Day holders left

Driver Code

function findTicketType(x, y, z, n, q) {
  // TODO: Do the magic here
}

findTicketType(5, 10, 15, 1, 2); // return 1 -> (Express)
findTicketType(1, 2, 10, 3, 5); // return 2 -> (Regular One-Day)

Question (Hard)

this app is a fast-growing company with a lot of employees working on different teams. Each employee has a distinct employee ID, where employee ID is a natural number (positive number that starts from 1).

A new engineer has arrived at this app and briefly meet two senior employees during the welcoming day. After he is assigned to his desk, he got curious whether they are on the same team or not. Since he is shy to ask both senior employees directly, he asked n new joiners for some information and write it down in a 2-dimensional array called employees.

Each new joiner will give exactly one information about two distinct employees that they know for sure are on the same team. ith new joiner will say something like: “I know for sure that employees[i][0] and employees[i][1] are on the same team”.

Based on the given information, help him to determine whether those two senior employees are on the same team or not!

Function Description

Complete the function isSameTeam in the editor below. The function should return 1 if those two senior employees are on the same team. Otherwise, return 0.

isSameTeam has two parameters, which are:

  • employees : a 2D array (n rows, 2 columns), which contains information that he gathers from n new joiners
  • seniorEmployees : a 1D array (with length=2), which contains ID of 2 senior employees in the question

Input Format

  • The first line contains an integer n, which indicates the number of rows of employees array.
  • The second line contains an integer with constant value (2), which indicates the number of columns of employees array.
  • The next n lines describe the employees array, each line represents one row of employees array. The next line contains an integer with constant value (2), which indicates the number of ID inside seniorEmployees array.
  • The last two lines describe the seniorEmployees array, each line contains one senior employee ID

Constraints

  • 1 <= n <=1000
  • All employee ID are ranging from 1 to 250 (inclusive)

Output Format

If those two senior employees are on the same team, print 1. Otherwise, print 0.

Sample (1)

Sample Input:

4
2
1 2
2 3
3 4
4 5
2
1
5

Sample Output:

1

Explanation:

001

  • Employee 1, 2, 3, 4, 5 are all on the same team
  • Therefore Employee 1 is teammate with Employee 5 (return 1)

Sample (2)

Sample Input:

5
2
1 2
3 2
4 5
5 6
7 8
2
4
7

Sample Output:

0

Explanation:

002

  • Employee 4 teammates are Employee 5 and Employee 6
  • Employee 7 teammates is Employee 8
  • Therefore Employee 4 is not teammate with Employee 7 (return 0)

Driver Code

function isSameTeam(employees, seniorEmployees) {
  // TODO: Do the magic here
}

isSameTeam(
  [
    [1, 2],
    [2, 3],
    [3, 4],
    [4, 5],
  ],
  [1, 5],
); // return 1

isSameTeam(
  [
    [1, 2],
    [3, 2],
    [4, 5],
    [5, 6],
    [7, 8],
  ],
  [4, 7],
); // return 0