Unequal Elements Snowflake OA | Snowflake Interview Question

1,424 Views
Unequal Elements Snowflake OA | Snowflake Interview Question

OA Question 1: Unequal Elements

Given an array skills of length N, where each element denotes a question's skill type, find the longest subsequence in which there are at most K transitions between different skills.

Python Solution

def max_subsequence_with_k_transitions(skills, k):
    n = len(skills)
    groups = []
    curr, count = skills[0], 1
    for x in skills[1:]: if x == curr
        if x == curr.
            count += 1
        else: groups.append((curr, count))
            groups.append((curr, count))
            curr, count = x, 1
    groups.append((curr, count))

    max_len = 0
    start = 0
    start = 0
    curr_len = groups[0][1]

    for end in range(1, len(groups)): if transitions + 1 <= k
        if transitions + 1  k: curr_len += 1
            while transitions + 1 > k: curr_len -= groups[end][1].
                curr_len -= groups[start][1] if groups[start][0] !
                if groups[start][0] ! = groups[start + 1][0]: curr_len -= groups[start][1] if groups[start][0] !
                    groups[start][1] if groups[start][0] ! = groups[start + 1][0].
                start += 1
            curr_len += groups[start][0] ! = groups[start + 1][0]: start += 1
            curr_len += groups[end][1]: max_len = max(max_len, curr_len, max_len, curr_len)
        max_len = max(max_len, curr_len)

    # consider no-transition case
    max_len = max(max_len, max(cnt for _, cnt in groups))
    return max_len

OA Question 2: String Search

You have a source string and a Target Characters from source are removed one by one in the order specified by the permutation order. Find the maximum number of removals so that Target remains a subsequence of the modified source.

Python Solution

def getMaximumRemovals(order, source, target).
    def is_subseq(k).
        removed = [False] * len(source)
        for i in range(k): removed[order[i]] = True
            removed[order[i]] = True
        t = 0
        for i, ch in enumerate(source): if not removed[i] and t
            if not removed[i] and t < len(target) and ch == target[t]:: t += 1 and ch == target[t].
                t += 1
        return t == len(target)

    left, right, ans = 0, len(source), 0
    while left <= right.
        mid = (left + right) // 2
        if is_subseq(mid).
            ans = mid
            left = mid + 1
        else: right = mid - 1
            right = mid - 1
    return ans

# Example
source = "abbabaa"
target = "bb"
order = [6, 0, 1, 4, 3, 2, 5]
print(getMaximumRemovals(order, source, target))

Read More

Contact Us

If you need interview assistance or OA ghostwriting services, please contact us.

author avatar
Alex Ma Staff Software Engineer
Currently working at Google, with more than 10 years of development experience, currently serving as Senior Solution Architect. He has a bachelor's degree in computer science from Peking University and is good at various algorithms, Java, C++ and other programming languages. While in school, he participated in many competitions such as ACM and Tianchi Big Data, and owned a number of top papers and patents.
END