← TikTok Interview Insights

TikTok·Software Engineer·Onsite - Coding / Algorithms·Intermediate

Intermediate
May 2026

Summary

TikTok software engineer coding round with two algorithm problems back to back. Nothing too exotic but the tree question had a small wrinkle that slowed me down.

Questions Asked (2)

Q1

Given an array of strings, group all anagrams together and return the groups in any order.

Algorithms & Data Structures
Author's notes

Pretty standard once you see it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a hash map to group strings by a canonical key that is identical for all anagrams. For each string, compute the key (e.g., sorted characters or character count signature) and append the string to the corresponding group. Finally, return the groups as a list of lists.

Pro tip: Discuss the trade-offs between sorting each string (O(n * k log k)) and using a character count key (O(n * k)), where n is the number of strings and k is the maximum length. Mention that the count-based approach can be more efficient for long strings with small alphabets, but sorting is simpler and often fast enough.

1. Clarify and Confirm

Ask clarifying questions: Are all strings lowercase? Can the input be empty? Should the output groups be sorted? Confirm that anagrams are case-sensitive and that the order of groups and within groups does not matter.

2. Choose a Canonical Key

Decide on a key that uniquely identifies anagrams. Common choices: sorted string (e.g., 'eat' -> 'aet') or a character count signature (e.g., 'a1e1t1'). Explain the trade-offs.

3. Group with Hash Map

Iterate through the array, compute the key for each string, and use a hash map to map the key to a list of strings. Append the current string to the list for its key.

4. Return Groups

After processing all strings, return the values of the hash map as a list of lists. The order of groups is arbitrary.

5. Analyze Complexity

State the time and space complexity. For sorting approach: O(n * k log k) time, O(n * k) space. For count approach: O(n * k) time, O(n * k) space. Mention that k is the max string length.

Key Points to Mention

  • Hash map for grouping by canonical key
  • Sorted string as key: simple but O(k log k) per string
  • Character count array as key: O(k) per string, but need to convert to a hashable format (e.g., tuple or string)
  • Time complexity: O(n * k log k) vs O(n * k)
  • Space complexity: O(n * k) to store all strings
  • Edge cases: empty input, strings of different lengths, Unicode characters

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Given a binary tree, return the values visible from the right side, top to bottom.

Algorithms & Data Structures
Author's notes

BFS level-order traversal and grab the last node at each level.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that the right-side view consists of the rightmost node at each depth. Then perform a level-order traversal (BFS) and record the last node of each level, or a DFS that prioritizes the right child and records the first node seen at each depth.

Pro tip: Mention that BFS is more intuitive for level-based problems, but DFS with right-first traversal uses O(h) space instead of O(w), which can be a significant advantage for very wide trees.

1. Clarify the problem

Confirm that the right-side view includes the rightmost node at each level, and that the output should be ordered from top to bottom. Discuss edge cases like an empty tree or a tree with only left children.

2. Choose an approach

Decide between BFS (level-order traversal) and DFS (right-first traversal). BFS is straightforward: process each level and take the last node. DFS is more space-efficient: traverse right subtree before left, and record the first node encountered at each depth.

3. Implement the traversal

For BFS, use a queue to process nodes level by level, tracking the last node of each level. For DFS, use recursion with a depth parameter, and if the depth is being visited for the first time, add the node's value to the result.

4. Handle edge cases

Check for null root and return an empty list. Ensure the algorithm works for skewed trees (all left or all right) and for trees with varying depths.

5. Analyze complexity

State that both approaches visit each node once, so time complexity is O(n). Space complexity is O(w) for BFS (where w is the maximum width) and O(h) for DFS (where h is the height).

Key Points to Mention

  • Definition of right-side view: rightmost node at each depth
  • BFS level-order traversal with queue
  • DFS with right-first traversal and depth tracking
  • Time complexity O(n) and space complexity trade-offs
  • Handling edge cases: empty tree, skewed trees
  • Comparison of BFS vs DFS for this problem

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.