Unequal Elements Snowflake OA | Snowflake Interview Question

1,610次閱讀
No Comments

Snowflake OA tests more than basic coding skills. It emphasizes problem decomposition, edge-case handling, and algorithmic efficiency. This article reviews two real Snowflake OA questions, explaining the key ideas, solution strategies, and common pitfalls to help candidates prepare effectively.

Snowflake OA

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:
            count += 1
        else:
            groups.append((curr, count))
            curr, count = x, 1
    groups.append((curr, count))

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

    for end in range(1, len(groups)):
        if transitions + 1 <= k:
            transitions += 1
            curr_len += groups[end][1]
        else:
            while transitions + 1 > k:
                curr_len -= groups[start][1]
                if groups[start][0] != groups[start + 1][0]:
                    transitions -= 1
                start += 1
            transitions += 1
            curr_len += groups[end][1]
        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 string. 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
        t = 0
        for i, ch in enumerate(source):
            if not removed[i] and t < len(target) 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
    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
目前就职于Google,10余年开发经验,目前担任Senior Solution Architect职位,北大计算机本硕,擅长各种算法、Java、C++等编程语言。在学校期间多次参加ACM、天池大数据等多项比赛,拥有多项顶级paper、专利等。
正文完
 0
评论(No Comments)