Remove duplicates and find constrained longest subsequence
Task 1: Remove duplicates from a linked list
Given the head of a singly linked list (not necessarily sorted), remove duplicate values so that each value appears only once. The solution must keep the first occurrence of each value and preserve the original relative order of the remaining nodes.
- Input:
headof a singly linked list where each node has fieldsvalandnext. - Output: The head of the de-duplicated linked list.
- Constraints (example): Number of nodes
nup to10^5; node values fit in a 32-bit signed integer.
Example
- Input:
1 -> 3 -> 1 -> 2 -> 3 -> null - Output:
1 -> 3 -> 2 -> null
Task 2: Longest subsequence of x that is a substring of y
Given two strings x and y, find one longest string s such that:
sis a subsequence ofx(characters chosen in order, not necessarily contiguous), andsis a substring ofy(contiguous segment ofy).
If multiple answers have the same maximum length, you may return any one of them.
- Input: Strings
x,y - Output: A string
smeeting the conditions with maximum possible length (or an empty string if no non-empty solution exists). - Constraints (example):
1 ≤ |x|, |y| ≤ 2000; characters are lowercase English letters.
Example
x = "abac",y = "zzbaczz"- One valid output:
"bac"(it is a subsequence ofxand a substring ofy).
AI interview prep
Core dilemma
Balancing algorithmic efficiency across distinct problem types, specifically optimizing for both space and time complexity in linked list manipulation and complex string matching.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Remove duplicates and find constrained longest subsequence
Task 1: Remove duplicates from a linked list
Given the head of a singly linked list (not necessarily sorted), remove duplicate values so that each value appears only once. The solution must keep the first occurrence of each value and preserve the original relative order of the remaining nodes.
- Input:
headof a singly linked list where each node has fieldsvalandnext. - Output: The head of the de-duplicated linked list.
- Constraints (example): Number of nodes
nup to10^5; node values fit in a 32-bit signed integer.
Example
- Input:
1 -> 3 -> 1 -> 2 -> 3 -> null - Output:
1 -> 3 -> 2 -> null
Task 2: Longest subsequence of x that is a substring of y
Given two strings x and y, find one longest string s such that:
sis a subsequence ofx(characters chosen in order, not necessarily contiguous), andsis a substring ofy(contiguous segment ofy).
If multiple answers have the same maximum length, you may return any one of them.
- Input: Strings
x,y - Output: A string
smeeting the conditions with maximum possible length (or an empty string if no non-empty solution exists). - Constraints (example):
1 ≤ |x|, |y| ≤ 2000; characters are lowercase English letters.
Example
x = "abac",y = "zzbaczz"- One valid output:
"bac"(it is a subsequence ofxand a substring ofy).