Find minimum cost in string
Last updated: December 21, 2025
Quick Overview
Given a string consisting of lowercase letters, each with an associated cost, find the minimum total cost to create a substring that contains all unique characters from the string. The input will be a string and a list of integers representing the costs for each character, and the output should be the minimum cost as an integer.
TikTok
December 21, 2025114
4
1,628 solved
Given a string consisting of lowercase letters, each with an associated cost, find the minimum total cost to create a substring that contains all unique characters from the string. The input will be a string and a list of integers representing the costs for each character, and the output should be the minimum cost as an integer.
This coding problem is frequently asked during Onsite at TikTok. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. TikTok expects candidates to write production-quality code, not just solve the puzzle.
What the Interviewer Expects
- Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
- Discuss multiple approaches and trade-offs before coding
- Implement an optimal solution with clean, production-quality code
- Handle all edge cases including boundary conditions and invalid input
- Optimize both time and space complexity with clear justification
- Test your solution systematically with well-chosen examples
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you parallelize this solution?
- What is the worst-case input for your solution?
- How would your solution change if the input was sorted?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Problem Analysis
To solve the problem of finding the minimum cost to create a substring containing all unique characters from a given string, we can identify that this is a variation of the 'Minimum Window Substring' ...
Approach
- Identify Unique Characters: First, traverse the input string to identify all unique characters and build a set of these characters.
- Use Two Pointers for Sliding Window: Initialize two po...