PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/TikTok

Minimize total factory cost with distance penalties

Last updated: Mar 29, 2026

Quick Overview

This question evaluates dynamic programming and sequence optimization skills, focusing on modeling cumulative build costs with adjacency distance penalties and handling variants such as skipping a factory.

  • medium
  • TikTok
  • Coding & Algorithms
  • Software Engineer

Minimize total factory cost with distance penalties

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You must choose construction options for factories to minimize a total cost. Input format: - `options[i]` is a list of options for factory `i`. - Each option is a pair `[cost, distance]`. For 3 factories, an example input is: ```python options = [ [[10, 0], [20, 0], [35, 0]], [[35, 0], [50, 0], [25, 0]], [[30, 0], [5, 0], [40, 0]] ] ``` ## Total cost (for N factories) If you pick exactly one option per built factory, define: - Build cost = sum of chosen `cost`. - Distance penalty = sum over adjacent built factories of `abs(distance[i] - distance[i+1])`. Total = build cost + distance penalty. ## Questions 1. **3 factories, ignore distance**: All distances are 0 (or distance penalties are ignored). Compute the minimum possible total cost. 2. **3 factories, include distance**: Use the full total cost formula. Compute the minimum. 3. **N factories, include distance**: Generalize to any `N` factories. Compute the minimum efficiently. 4. **N factories, must skip exactly one factory**: You must choose options for exactly `N-1` factories, skipping (not building) exactly one factory. Distance penalty applies between adjacent built factories in their original order (i.e., skipping connects its neighbors). Compute the minimum. ## Output Return the minimum total cost (and optionally the chosen options if requested). ## Constraints / Notes - Assume `N` and number of options per factory can be large enough that brute force over all combinations may be too slow. - Discuss time/space complexity tradeoffs.

Quick Answer: This question evaluates dynamic programming and sequence optimization skills, focusing on modeling cumulative build costs with adjacency distance penalties and handling variants such as skipping a factory.

Related Interview Questions

  • Parse a nested list from a string - TikTok (medium)
  • Implement stacks, streaming median, and upward path sum - TikTok (easy)
  • Solve common string/DP/stack problems - TikTok (medium)
  • Implement stack variants and path-sum check - TikTok (medium)
  • Maximize sum with no adjacent elements - TikTok (medium)
TikTok logo
TikTok
Jan 6, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
3
0
Loading...

You must choose construction options for factories to minimize a total cost.

Input format:

  • options[i] is a list of options for factory i .
  • Each option is a pair [cost, distance] .

For 3 factories, an example input is:

options = [
  [[10, 0], [20, 0], [35, 0]],
  [[35, 0], [50, 0], [25, 0]],
  [[30, 0], [5, 0], [40, 0]]
]

Total cost (for N factories)

If you pick exactly one option per built factory, define:

  • Build cost = sum of chosen cost .
  • Distance penalty = sum over adjacent built factories of abs(distance[i] - distance[i+1]) .

Total = build cost + distance penalty.

Questions

  1. 3 factories, ignore distance : All distances are 0 (or distance penalties are ignored). Compute the minimum possible total cost.
  2. 3 factories, include distance : Use the full total cost formula. Compute the minimum.
  3. N factories, include distance : Generalize to any N factories. Compute the minimum efficiently.
  4. N factories, must skip exactly one factory : You must choose options for exactly N-1 factories, skipping (not building) exactly one factory. Distance penalty applies between adjacent built factories in their original order (i.e., skipping connects its neighbors). Compute the minimum.

Output

Return the minimum total cost (and optionally the chosen options if requested).

Constraints / Notes

  • Assume N and number of options per factory can be large enough that brute force over all combinations may be too slow.
  • Discuss time/space complexity tradeoffs.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More TikTok•More Software Engineer•TikTok Software Engineer•TikTok Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.