TikTok Intern OA rounds are starting to roll out again in batches. After opening the assessment, the overall problem style felt fairly familiar, and I was able to move through it relatively quickly. A typical OA has four questions. Below are the two questions from this round that are worth documenting, especially if you are preparing for the TikTok Intern process.
Question 1: Sort Each k-Border and Write It Back Clockwise
Given an n × n integer matrix, define the 0-border as the outermost layer consisting of the first row, last row, first column, and last column.
After removing the 0-border, the outer layer of the remaining matrix becomes the 1-border. The same definition applies to 2-border, 3-border, and so on, up to floor((n - 1) / 2).
For every k-border, extract all elements in that layer, sort them, and then write them back to their original positions starting from the top-left corner and proceeding in clockwise order.
Approach
The simplest implementation is to process the matrix layer by layer.
For each layer, calculate top, bottom, left, and right. Then collect the elements in this order:
- Top row: left → right
- Right column: top + 1 → bottom
- Bottom row: right - 1 → left
- Left column: bottom - 1 → top + 1
This avoids counting the four corners more than once. After collecting the values, simply sort the array and write the sorted values back using the exact same traversal order.
The problem does not require an especially optimized solution, so an O(n³) implementation can still pass. The main thing to watch is indexing, especially when the innermost layer contains only one element.
Question 2: How Many Segments Remain After Removing Houses?
You are given several houses located at distinct integer positions on a number line. The initial positions are stored in houses. The queries array gives the order in which houses are removed.
After removing each house, return the number of remaining house segments. A segment consists of one or more houses occupying consecutive positions. A single isolated house also counts as one segment.
For example:
houses = [1, 2, 3, 6, 7, 9]
queries = [6, 3, 7, 2, 9, 1]
output = [3, 3, 2, 2, 1, 0]
Approach
A clean solution is to maintain the current number of segments while processing the queries from left to right.
First, calculate the initial number of segments from all houses. Then store the currently active house positions in a set or ordered data structure.
When removing a house at position x, only its immediate neighbors x - 1 and x + 1 matter:
- Both neighbors exist: one segment is split into two, so the segment count increases by 1.
- Only one neighbor exists: the house was at the end of a segment, so the count stays unchanged.
- Neither neighbor exists: the house was an isolated segment, so the count decreases by 1.
After processing each query, append the current segment count to the answer array.
The key observation is that removing one position only affects its two adjacent positions, so there is no need to scan the entire array after every deletion. This gives an efficient implementation with roughly O(n + q) expected time when using a hash set.
Overall Takeaway
TikTok Intern OAs often have four questions with a relatively tight time limit, but the difficulty is usually manageable if you are familiar with common implementation patterns. These two problems are good examples: the first is mainly matrix simulation plus sorting, while the second is about maintaining connected segments under deletions.
If you are preparing for a TikTok Intern OA, it is worth reviewing matrix traversal, simulation, hash sets, sorting, and simple dynamic connectivity patterns before starting the assessment. When the question looks familiar, recognizing the underlying pattern quickly can save a lot of time.
Need More TikTok OA & Interview Preparation?
If you are currently preparing for TikTok, Meta, Google, Amazon, or other North American tech interviews, Interview Show provides interview preparation and assistance services covering OA preparation, coding practice, system design, mock interviews, and Virtual Onsite preparation.
You can visit Interview Show to learn more about the available services and get personalized preparation support for your upcoming technical interviews.
Top comments (0)