
2025.March latest tiktok interview experience, let's take a look at it~
Tiktok three rounds first round second round are coding, two questions per round. bq is about a project, followup difficulties encountered in the project. hm interview resume + coding. resume asked infra, coding is dp.
Code
Question 1
Whether the array is ordered under the condition that it is allowed to modify at most one element of the array.
Consider traversing the array from front to back, will encounter elements nums[i] that do not satisfy the ordered condition, i.e., nums[i] < nums[i-1], if i is size-1 i.e., the last element, or the array is already all ordered, return true directly, otherwise the size of nums[i+1] should be considered:
- If nums[i+1] >= nums[i-1], such as the array 0, 1, 2, [1], 3... in 3 > 2, then i+1 and the elements before it satisfy the conditions of the question, and return true if the latter is ordered;
- Otherwise, if 3 < 4 in the array 0, 1, 4, [1], 3..., it is sufficient if you change nums[i-1] to nums[i], and then traverse the array from i-2 to determine whether it is ordered.
Time complexity O(n), Space complexity O(1)
reasoning
- Iterate through the array to find the first position that violates non-decreasing order
i
(i.e.)nums[i]
<nums[i-1]
). - If i has traversed to the end of the array or to the penultimate element, it means that at most one element is out of order and can be fixed by modifying that element and returning directly to the
true
. - Check if it is possible to modify the
nums[i-1]
maybenums[i]
to fix the array. - from position
i
Starts and continues traversing the array backward, checking to see if all elements satisfy non-decreasing order. If any elements are found to be out of order, return thefalse
. - If the entire array satisfies non-decreasing order, return
true
.
class Solution.
def checkPossibility(self, nums): i = 0
i = 0
for i in range(1, len(nums)): if nums[i] < nums[i-1]: if i = 0
if nums[i] < nums[i-1].
if nums[i] = len(nums) - 1: return True
if i >= len(nums) - 1: return True
if nums[i + 1] >= nums[i - 1].
i += 1
else: nums[i - 1] = nums[i - 1]: i += 1
nums[i - 1] = nums[i]
i = max(0, i-2)
for i in range(i, len(nums) - 1).
if nums[i] > nums[i + 1].
if nums[i] > nums[i + 1]: return False
if nums[i] > nums[i + 1]: return False
Question 2
Find the node in the tree that has the smallest average distance to other nodes, an edge distance counts as 1 and requires a time complexity of O(n). consultation
reasoning
- First, define a flag variable
visited
, which is used to mark whether the target node p has been found, with an initial value offalse
. - Define an empty stack
stack
, which is used to store the values of the nodes on the path from the root node to the target node p. - Define a function
getDisToPar
, receives three parameters: the current noderoot
Target nodesp
and a stack for storing pathsstack
. - exist
getDisToPar
function, first check whether the current node is empty, if it is empty, then directly return. - Adds the value of the current node to the stack.
- If the current node is the target node p and has not been previously accessed (
visited
because offalse
), then thevisited
mark sth. astrue
and return. - If the target node p has not been found yet, look recursively in the left subtree first.
- If it is not found in the left subtree, then recursively look in the right subtree.
- If neither the left nor right subtree of the current node finds the target node p, it means that the current node is not on the path of the target node p. Pop it off the stack.
- At the end of the function run, the stack stores the values of the nodes on the path from the root node to the target node p.
visited = False
stack = []
def getDisToPar(root, p, stack):
global visited
if root is None: return
return
# Add node to stack
stack.append(root.val)
# If found
if not visited and root == p.
visited = True
if not visited and root == p: visited = True
# Find left subtree first
if not visited: getDisToPar(root.left, p, stack)
getDisToPar(root.left, p, stack)
# Left subtree not found then right subtree
if not visited: getDisToPar(root.left, p, stack)
getDisToPar(root.right, p, stack)
# If not found, it is not under this node, pop it up
if not visited: getDisToPar(root.right, p, stack)
stack.pop()
return
After our strong interview assistance, OA ghostwriting for hire for masters, candidates through the analysis and communication of these interview questions, the interviewer not only understands the candidate's programming ability, but also sees my clear thinking and effective communication skills in the problem solving process. These not only help to cope with tiktok interview, but also enhance our ability to solve practical programming problems. I wish you all good luck in your interviews!
If you also need our interview assistance services, please Contact Us Now.