TikTok logo

TikTok Medium Interview Questions

13 medium-level practice questions for TikTok technical interviews

TikTok software engineer interviews cover algorithms, data structures, system design, and coding problems drawn from real interview rounds.

Software Engineer Backend Engineer Frontend Engineer Full Stack Engineer Mobile Engineer Data Engineer Data Scientist ML Engineer DevOps Engineer DevOps Engineer Product Manager SRE Security Engineer Engineering Manager Data Analyst UX/UI Designer QA Engineer
coding Medium Verified Question #1

1. Region Grid Coloring


Category: Grid/matrix coding problem
You are given an M x N grid of security zones. Each cell contains one of the following values: - 1 -- the zone is cleared - 0 -- the zone...
Input: 2D grid
Output: Computed result
coding Medium Verified Question #2

2. Circular Signal Window


Category: Array coding problem
You are given a circular array signal of 0s and 1s representing antenna readings logged in sequence, where 1 means good signal and 0 means...
Input: Array
Output: Integer
coding Medium Verified Question #3

3. Pattern First Occurrence


Category: String coding problem
You are given a search pattern and a log string, both consisting of uppercase letters and the wildcard character *. The * wildcard matches...
Input: String
Output: Computed result
coding Medium Verified Question #4

4. [CodeSignal] Minimum Score Suppressor


Category: Algorithm coding problem
You are given a list of ad budgets (integers) and an integer k representing the number of throttle operations available. Each operation selects...
Input: List
Output: Integer
coding Medium Verified Question #5

5. [CodeSignal] Optimal Voucher Allocation


Category: Algorithm coding problem
You are given couponsCount discount coupons and a list of monthly service fees. Each coupon halves one service fee using integer floor division....
Input: List
Output: Integer
coding Medium Verified Question #6

6. Remove Consecutive for Balance


Category: Array coding problem
You are given a list of track lengths tracks and a positive integer k. Remove exactly k consecutive tracks from the list so that the sum of...
Input: Array
Output: Computed result
coding Medium Verified Question #7

7. Path Prefix Router


Category: String coding problem
Design a CDN route manager class CdnRouter that maps URL paths to origin servers using longest-prefix matching. Implement the following methods: -...
Input: String
Output: Computed result
coding Medium Verified Question #8

8. Bounded Repeat Substring


Category: String coding problem
A sensor data stream is represented as a string of characters. A contiguous segment of the stream is considered valid if it contains no three...
Input: String
Output: Computed result
coding Medium two pointers #1

1. Two Pointers — Find Valid Video Pair

Background: TikTok relies on understanding user preferences to suggest relevant content. This problem is related to the algorithm used to find complementary videos for recommendations.
Problem statement: Given an array of n integers representing the duration of videos, your task is to find if there exists a pair of videos such that their duration sums up to a target value targetDuration. Return the indices of these two videos in an array.
Function/class signature:
  • def find_video_pair(videos: List[int], targetDuration: int) -> List[int]:


Example 1:
Input: videos = [20, 15, 30, 10, 25], targetDuration = 35
Output: [0, 2]
Explanation: The videos with durations 20 and 15 sum up to 35.
Example 2:
Input: videos = [10, 5, 7, 8], targetDuration = 17
Output: [1, 3]
Explanation: The videos with durations 5 and 12 sum up to 17.
Constraints:
  • 2 <= n <= 10^4

  • 1 <= videos[i] <= 10^3

  • 1 <= targetDuration <= 2 * 10^3


coding Medium dynamic programming #2

2. Dynamic Programming — Minimum Cost to Reach Target

Background: In TikTok, users often browse content that requires a recommendation algorithm to ensure efficient and personalized video feeds. The algorithm should minimize the cost of obtaining trending videos while reaching a specific target threshold of views.
Problem statement: You are given an integer array cost where cost[i] is the cost to obtain a video at index i. You also have a target integer targetViews, which is the number of total views you want to achieve. Your task is to determine the minimum cost to obtain videos from the cost array such that the total number of views reaches or exceeds targetViews. Return the minimum cost. If it is impossible to achieve the target, return -1.
Function signature:
  • def minCost(cost: List[int], targetViews: int) -> int:

Example 1:
Input: cost = [1,2,3,4,5], targetViews = 5
Output: 5
Explanation: You can obtain the video at index 4 for 5 cost, achieving exactly 5 views.
Example 2:
Input: cost = [1,2,3], targetViews = 6
Output: -1
Explanation: The total views obtainable from the videos are less than 6.
Constraints:
  • 1 <= len(cost) <= 100

  • 1 <= cost[i] <= 1000

  • 0 <= targetViews <= 1000

  • The sum of all elements in cost is guaranteed to be greater than or equal to targetViews if possible.
coding Medium heap #3

3. CODING — Find the Most Watched Videos

Background: In TikTok, understanding user engagement is critical. Analyzing the most watched videos can help in content recommendation and improving user satisfaction.
Problem statement: Given an array of videoIds representing the videos that users watched and a corresponding array of viewCounts that represents how many times each video was watched, you need to write a function that finds the k most watched videos. The function should return a list of video IDs in descending order of views.
Function/class signature:
  • def most_watched_videos(videoIds: List[str], viewCounts: List[int], k: int) -> List[str]:


Example 1:
  • Input: videoIds = ['v1', 'v2', 'v3', 'v4'], viewCounts = [100, 250, 150, 250], k = 2

  • Output: ['v2', 'v4']

  • Explanation: Videos v2 and v4 have the highest views (250).


Example 2:
  • Input: videoIds = ['a', 'b', 'c', 'd'], viewCounts = [20, 10, 30, 30], k = 3

  • Output: ['c', 'd', 'a']

  • Explanation: Videos c and d both have 30 views, and a has 20 views.


Constraints:
  • 1 <= len(videoIds), len(viewCounts) <= 10^5

  • 1 <= viewCounts[i] <= 10^6

  • 1 <= k <= len(videoIds)
coding Medium hash map #4

4. Hash Map — Implement a video metadata cache

Background: TikTok needs to efficiently handle a large volume of video metadata requests while minimizing server load. An effective caching mechanism can greatly improve response times for users when accessing popular videos.
Problem statement: Implement a class VideoMetadataCache that can store and retrieve video metadata based on video IDs. The cache should automatically evict the least recently used (LRU) entry when it reaches a maximum capacity. You need to implement two methods: get(video_id: str) -> Optional[dict] to retrieve metadata and put(video_id: str, metadata: dict) -> None to store metadata.
Function/class signature:
  • def get(video_id: str) -> Optional[dict]:

  • def put(video_id: str, metadata: dict) -> None:

Example 1:
Input: put('v1', {'title': 'Video 1', 'views': 1000})
Output: None
Input: get('v1')
Output: {'title': 'Video 1', 'views': 1000}
Explanation: The metadata for video ID 'v1' is successfully retrieved.
Example 2:
Input: put('v2', {'title': 'Video 2', 'views': 2000})
Input: put('v3', {'title': 'Video 3', 'views': 3000})
Input: put('v4', {'title': 'Video 4', 'views': 4000})
Input: get('v1')
Output: None
Explanation: 'v1' was evicted when adding 'v4' as it was the least recently used.
Constraints:
  • Maximum cache size: 1000 entries

  • Video ID will always be a non-empty string

  • Metadata is a dictionary containing 'title' as a string and 'views' as an integer.
coding Medium graph #5

5. GRAPH — Find Shortest Path for Video Recommendations

Background: TikTok needs to optimize video recommendations to enhance user engagement. A graph can efficiently represent the relationships between videos based on user interactions, such as likes and shares.
Problem statement: You are tasked with implementing a function that finds the shortest path (in terms of user views) in a graph of videos. Each video is represented as a node, and user interactions can be depicted as edges connecting these nodes. Your function should return the shortest path from a start video to a target video.
Function/class signature:
  • def shortest_path_videos(graph: Dict[str, List[str]], start: str, target: str) -> List[str]:

Example 1:
  • Input: graph = {"A": ["B", "C"], "B": ["C", "D"], "C": ["D"], "D": []}; start = "A"; target = "D"

  • Output: ['A', 'B', 'D']

  • Explanation: The path from A to D is A -> B -> D, which has the fewest edges.

Example 2:
  • Input: graph = {"A": ["B", "C"], "B": ["E"], "C": ["D", "E"], "D": [], "E": []}; start = "A"; target = "E"

  • Output: ['A', 'B', 'E']

  • Explanation: The path from A to E is A -> B -> E, one of the shortest paths available.

Constraints:
  • 1 <= |graph| <= 1000

  • Each video name is unique.

  • The path must be valid, if a path does not exist, return an empty list.

Start practicing TikTok questions

Sign up for free to access walkthroughs, AI-generated questions, and more.

Get Started Free