DIY: Sequence Reconstruction
Solve the interview question "Sequence Reconstruction" in this lesson.
We'll cover the following
Problem statement
Check whether the original sequence org
can be uniquely reconstructed from the sequences in seqs
. The org
sequence is a permutation of the integers from 1
to n
. Reconstruction means building a shortest common supersequence of the sequences in seqs
(i.e., the shortest sequence so that all sequences in seqs
are subsequences of it). Determine whether there is only one sequence that can be reconstructed from seqs
, and it is the org
sequence.
Input
The input will be an array of integers, org
, and another nested RRAY of integers, seqs
. The following is an example input:
org = {1,2,3}
seqs = {{1,2},{1,3},{2,3}}
Output
The output will be a Boolean value representing whether org
can be uniquely constructed from seqs
. The following is just an example of what the output of the above input will look like:
true
{1,2}
,{1,3}
, and {2,3}
are unique subsequences of org
.
Coding exercise
Implement the function sequenceReconstruction(org, seqs)
, where org
is the array and seqs
is the array of arrays. The function will return a Boolean value representing whether org
can be uniquely constructed from seqs
.
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.