0% found this document useful (0 votes)
32 views6 pages

Coding Challenges2 Level of Difficulty Medium HIGH

Uploaded by

thanneruunnathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views6 pages

Coding Challenges2 Level of Difficulty Medium HIGH

Uploaded by

thanneruunnathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Coding Challenge1:

Scenario: Consolidating Inventory Data from Multiple Warehouses

A logistics company manages three warehouses: Warehouse A, Warehouse B, and Warehouse C.


Each warehouse maintains an inventory list of product IDs, sorted in ascending order. The company’s
operations manager needs to identify which product IDs are common across all three warehouses to
ensure these products are well-stocked and distributed appropriately.

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

• Array A (sorted): Product IDs from Warehouse A

• Array B (sorted): Product IDs from Warehouse B

• Array C (sorted): Product IDs from Warehouse C

For example:

A = [101, 105, 110, 115, 120]

B = [102, 105, 110, 115, 125]

C = [103, 105, 110, 115, 130]

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.

2. Compare elements at the current positions of all three pointers:

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.

3. Continue this until any one of the arrays is fully traversed.

Code Solution Using Python:

def find_common_elements(arr1, arr2, arr3):


i, j, k = 0, 0, 0 # Pointers for each array
common_elements = []

while i < len(arr1) and j < len(arr2) and k < len(arr3):


# If all elements are equal, add to result and move all pointers
if arr1[i] == arr2[j] == arr3[k]:
common_elements.append(arr1[i])
i += 1
j += 1
k += 1
# Move the pointer of the smallest element
elif arr1[i] < arr2[j]:
i += 1
elif arr2[j] < arr3[k]:
j += 1
else:
k += 1

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)

Test Case: If the input arrays are:

A = [10, 20, 30, 40, 50]

B = [15, 20, 30, 45, 60]

C = [5, 20, 30, 35, 70]

Output: [20, 30]

Coding challenge 2:

Problem Scenario: Closest Pair in a Sorted Array

Scenario: Smart Shopping Assistant

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.

2. A target value xxx representing the user's budget.

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).

2. Second line: nnn integers representing the sorted array.

Output Format

• Print the pair of integers whose sum is closest to xxx.

Test Cases

Input:

34

235

Output:

23

Explanation:
Possible pairs:

• 2+3=52 + 3 = 52+3=5 (closest to 4, difference = 1)


• 2+5=72 + 5 = 72+5=7 (difference = 3)
• 3+5=83 + 5 = 83+5=8 (difference = 4)

Closest pair: 222 and 333.

Coding Challenge3:

Problem Scenario: Maximum Consecutive Repeating Character in String

Scenario: Error Detection in Repetitive Manufacturing Codes

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

1. Iterate through the string:

o Track the current character and its count of consecutive repetitions.

o Update the maximum count and character when a higher count is found.

2. Return the result:

o The character with the highest count of consecutive repetitions.

Coding Challenge4:

Problem Scenario: Identifying the Celebrity

Scenario: Party Celebrity Identification


Imagine a party where guests interact based on specific rules:

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

Given a square matrix mat[][] of size n x n, where:

• mat[i][j]=1mat[i][j] = 1mat[i][j]=1: Person iii knows person jjj.

• mat[i][j]=0mat[i][j] = 0mat[i][j]=0: Person iii does not know person jjj.

The task is to identify the celebrity if one exists.

Input:

• A matrix mat[][] of size n×nn \times nn×n.


Output:

• The ID of the celebrity (0-based index) if present, or -1 if no celebrity exists.

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

prerequisites = {{1, 0}}

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

prerequisites = {{1, 0},

{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.

Expected Time Complexity: O(n+m).


Expected Auxiliary Space: O(n+m).

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:

Climbing stairs to reach the top:

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}.

You might also like