字节跳动 tiktok oa题目分享

更多资源与服务

想要了解更多编程面试技巧,或者需要专业的面试辅导OA代做简历润色等服务?我们提供全方位的求职面试支持,帮助您在大厂面试真题系统设计面试算法面试中脱颖而出,轻松拿到心仪的 offer!无论您是留学生、刚踏入职场的新人,还是需要代码优化建议的开发者,我们的团队由ACM奖牌得主、大厂资深 TLM 及经验丰富的行业老兵组成,确保为您提供最专业的指导。

扫描下方二维码,添加我们的微信,获取更多服务:

微信二维码

关键词:

  • 面试代面
  • 代码代写
  • OA代做
  • 面试技巧
  • 面试经验分享
  • 职业规划
  • 编程练习

让我们帮助您在技术面试中脱颖而出,实现职业上的飞跃!

Question 1: Counting Balanced Clips

Description:

Given a video clip of certain length and a difficulty tolerance, calculate the number of "balanced" clips possible. A clip is defined as a sequence of characters, and it is considered balanced if each consecutive pair of characters differs in their alphabet position by no more than a given tolerance diff.

Implement a function to return the total number of balanced clips possible, given the clip length and difficulty tolerance.

Input:

  • clipLength (int): An integer indicating the length of the clip.
  • diff (int): An integer defining the maximum allowed difference in position between consecutive characters.

Output:

  • Returns the count of balanced clips, modulo 10^9 + 7.

Code Example:

def countBalancedClips(clipLength, diff):
    dp = [[0] * 26 for _ in range(2)]
    MOD = 10**9 + 7
    for i in range(26):
        dp[0][i] = 1
    for i in range(1, clipLength):
        cur = i % 2
        prev = (i+1) % 2
        dp[cur] = [0] * 26
        for j in range(26):
            for k in range(26):
                if abs(j - k) <= diff:
                    dp[cur][j] = (dp[cur][j] + dp[prev][k]) % MOD
    return sum(dp[(clipLength - 1) % 2]) % MOD

Question 2: Minimizing TikTok Server Network Cost

Description:

Given coordinates of servers in a 2D plane, implement an efficient function to calculate the minimum network cost needed to connect all servers. The cost of connecting two servers is determined by the minimum of their x or y coordinate differences.

Use an algorithm to compute the minimal cost to connect the network of servers, where each server must be connected to at least one other server.

Input:

  • x (List[int]): List of x-coordinates of the servers.
  • y (List[int]): List of y-coordinates of the servers.

Output:

  • Returns the minimum network connection cost to connect all servers.

Code Example:

import heapq
from collections import defaultdict

def getMinTikTokServerNetworkCost(x, y):
    n = len(x)
    servers = [(x[i], y[i], i) for i in range(n)]
    g = defaultdict(list)
    for i in [0, 1]:
        servers.sort(key=lambda server: server[i])
        for j in range(1, n):
            x1, y1, idx1 = servers[j-1]
            x2, y2, idx2 = servers[j]
            cost = min(abs(x1 - x2), abs(y1 - y2))
            g[idx1].append((cost, idx2))
            g[idx2].append((cost, idx1))
    res = 0
    visited = set()
    h = [(0, 0)]
    while len(visited) < n:
        cost, cur = heapq.heappop(h)
        if cur in visited:
            continue
        visited.add(cur)
        res += cost
        for c, v in g[cur]:
            if v not in visited:
                heapq.heappush(h, (c, v))
    return res