Skip to content

ORIGINAL CODING QUESTION

Rank in merged sorted logs

Merge two nondecreasing logs conceptually. Return the k-th smallest value, with k one-based. Duplicates count separately.

Open practice workspace

Original exercise · hard. Editorial role relevance does not imply a company has asked this question.

Input and output

n, n sorted values, m, m sorted values, k.

Constraints

1 ≤ n,m ≤ 100; -100 ≤ value ≤ 100; 1 ≤ k ≤ n+m.

Example 1

Input

2 1 4 3 2 3 5 3

Output

3

The merged third value is 3.

Example 2

Input

2 1 1 2 1 2 3

Output

1

Duplicates make the third value 1.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

The two sorted streams define an ordered merge.

Hint 2

Avoid losing duplicate positions.

Hint 3

A partition-based search can skip the first k-1 entries without constructing a full merge.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →