Coding & Algorithms (3 tasks)
1) Find the 5th node from the end (singly linked list)
You are given the head pointer of a singly linked list.
-
Return the
value
of the node that is the
5th from the end
(i.e., counting from the tail: tail is 1st).
-
If the list has fewer than 5 nodes, specify a reasonable behavior (e.g., return
null
/ throw an error).
Constraints: Aim for O(n) time and O(1) extra space.
2) Remove “camel-pattern” triplets from a string
Given a string s (ASCII letters only), remove every contiguous length-3 substring s[i..i+2] that matches the pattern:
-
s[i] == s[i+2]
(exact same character), and
-
s[i]
and
s[i+1]
have
different letter cases
(one uppercase, one lowercase).
After removals, the remaining parts are concatenated.
Example
-
Input:
"AaADbDEeEbcvQv"
-
Output:
"bc"
Clarify whether removals are single pass or repeated until no more matches; design your solution accordingly.
Constraints: 1≤∣s∣≤2×105.
3) Gold bar daily pay puzzle
A worker works 7 days and is paid one gold bar total. The bar can be broken into pieces with at most two breaks.
-
How can you pay the worker
each day
such that the daily payment is equal (i.e., each day receives exactly
71
of the total bar value)?
-
You may exchange pieces back and forth each day (give/take pieces) as long as the net pay per day is correct.