Coding Challenges2 Level of Difficulty Medium HIGH
Coding Challenges2 Level of Difficulty Medium HIGH
Problem Statement
Given three sorted arrays representing the product IDs from Warehouse A, Warehouse B, and
Warehouse C, write a program to find the product IDs that are present in all three warehouses.
Input
For example:
Expected Output
The program should return the list of product IDs that are common in all three arrays.
Output: [105, 110, 115]
Approach
1. Use pointers to traverse all three arrays simultaneously since the arrays are sorted.
o If the elements are the same, add the element to the result list and increment all
three pointers.
o If the smallest element is less than others, move the pointer of the smallest element
to the next position.
return common_elements
# Example usage
A = [101, 105, 110, 115, 120]
B = [102, 105, 110, 115, 125]
C = [103, 105, 110, 115, 130]
result = find_common_elements(A, B, C)
print("Common Product IDs:", result)
Coding challenge 2:
An online shopping platform is developing a feature to recommend two products to customers based
on their budget. The feature should suggest a pair of products whose combined price is closest to
the user's budget without exceeding it.
The product prices are stored in a sorted array, and the system needs to identify the best pair
efficiently to provide quick recommendations.
Problem Description
Given:
1. A sorted array of integers representing product prices.
Find the pair of elements in the array whose sum is closest to xxx. If there are multiple pairs with the
same closest sum, return the first such pair.
Input Format
1. First line: Two integers nnn (number of elements in the array) and xxx (target sum).
Output Format
Test Cases
Input:
34
235
Output:
23
Explanation:
Possible pairs:
Coding Challenge3:
In a manufacturing process, products are tagged with unique binary or alphanumeric codes for
tracking. Sometimes, due to machine errors, the tags may contain unintended repetitive characters,
indicating potential defects. For quality assurance, engineers analyze the tags to identify the
maximum consecutive repeating character, as this can highlight potential issues in the printing
mechanism.
Example:
• A tag AAABBBCCDAA might indicate that the printing system paused while printing A or B.
• Identifying these repeating characters helps engineers fix the problem and ensure the
product quality.
Problem Description
Given a string strstrstr, find the character that appears consecutively the maximum number of
times.
Input:
A single string strstrstr.
Output:
The character with the highest consecutive repetition.
Approach to Solve
o Update the maximum count and character when a higher count is found.
Coding Challenge4:
1. If a person knows someone, they might have been introduced or interacted directly.
2. A celebrity at the party is someone who is known by everyone but doesn’t know anyone
else.
The task is to identify whether there is a celebrity at the party and return their ID (index) if one
exists. Otherwise, return -1.
Problem Description
Input:
Course Schedule
There are a total of n tasks you have to pick, labelled from 0 to n-1. Some tasks may
have prerequisite tasks, for example to pick task 0 you have to first finish tasks 1, which is expressed
as a pair: [0, 1]
Given the total number of n tasks and a list of prerequisite pairs of size m. Find a ordering of tasks
you should pick to finish all tasks.
Note: There may be multiple correct orders, you just need to return any one of them. If it is
impossible to finish all tasks, return an empty array. Driver code will print "No Ordering Possible", on
returning an empty array. Returning any correct order will give the output as 1, whereas any invalid
order will give the output 0.
Example 1:
Input:
n = 2, m = 1
Output:
Explanation:
The output 1 denotes that the order is valid. So, if you have, implemented your function correctly,
then output would be 1 for all test cases. One possible order is [0, 1].
Example 2:
Input:
n = 4, m = 4
{2, 0},
{3, 1},
{3, 2}}
Output:
Explanation:
There are a total of 4 tasks to pick. To pick task 3 you should have finished both tasks 1 and 2. Both
tasks 1 and 2 should be pick after you finished task 0. So one correct task order is [0, 1, 2, 3]. Another
correct ordering is [0, 2, 1, 3]. Returning any of these order will result in an output of 1.
Your Task:
The task is to complete the function findOrder() which takes two integers n, and m and a list of lists
of size m*2 denoting the prerequisite pairs as input and returns any correct order to finish all the
tasks. Return an empty array if it's impossible to finish all tasks.
Constraints:
1 ≤ n ≤ 105
0 ≤ m ≤ min(n*(n-1),105)
0 ≤ prerequisites[i][0], prerequisites[i][1] < n
All prerequisite pairs are unique
prerequisites[i][0] ≠ prerequisites[i][1]
Dynamic Programming:
There are n stairs, and a person standing at the bottom wants to climb stairs to reach the top. The
person can climb either 1 stair or 2 stairs at a time, the task is to count the number of ways that a
person can reach at the top.
Note: This problem is similar to Count ways to reach Nth stair (Order does not matter) with the only
difference that in this problem, we count all distinct ways where different orderings of the steps are
considered unique.
Examples:
Input: n = 1
Output: 1
Explanation: There is only one way to climb 1 stair.
Input: n = 2
Output: 2
Explanation: There are two ways to reach 2th stair: {1, 1} and {2}.
Input: n = 4
Output: 5
Explanation: There are five ways to reach 4th stair: {1, 1, 1, 1}, {1, 1, 2}, {2, 1, 1}, {1, 2, 1} and {2, 2}.