Facebook | onsite | matching processes
Company: Meta (Facebook)
Role: unknown
Interview Stage: Onsite
Location: unknown
Year: unknown
Original Post Content:
[The question had a long description and I am rephrasing]
Say we have a list of Strings contains an starting process Ai and an ending one Bi where i is an integer showing the id of the process. We can have multiple Start/End pairs which are shown by adding numbers after each one of these letters.
- an Starting process should always end with the equivalent End (every Ai should come before Bi)
- a valid path cannot have duplicate processes, like A1, B1, A1, B1
- both Ai and Bi should exist in the valid path
- This is very similar to matching ( with )
For example:
[A1, B1, A2, B2] is a valid process array since every Ai has ended with the same Bi.
[A1, A2, B2, B1] or [A1, A2, B1, B2] are also valid
[A1, A2, B1, B2] valid
[A1, B1, A2, B2] valid
[A1, B2, B1, A2] invalid
[A1, B2] invalid
[A1, A2] invalid
[A1, B1, B1] invalid
[] valid
[A1, A1, B1] invalid
[A1, A1, B1, B1] invalid
[A1, B1, A1] invalid
[A1, B1, A1, B1] invalid
Given a list of processes, find the longest valid process subarray you can find:
- [A1, B1, A2, B2] : return 4
- [A1, A2, B2] : return 2 (A2, B2)
- [A1, B1, A2, A1, B1, B2] : return 4 (A2, A1, B1, B2)
I tried to solve is via stack and similar to Longest Valid Parenthesis but it failed for cases where you have full match (A1, B1, A2, A1, B1, B2 vs A1, B1, A2, B2)