Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Discord DTLS Buffer Processing

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Discord DTLS Buffer Processing. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Discord's voice transport may need to assemble ordered DTLS message payloads into fixed-size buffers. Each emitted fragment has a fixed DTLS record overhead, so a buffer can contain fewer payload bytes when it contains more fragments.

Implement pack_dtls_messages(messages, buffer_size, record_overhead). Process messages in order, fragmenting messages when necessary, and return the minimum number of buffers required.

Formal Specification

  • messages is a list of non-negative integer payload lengths.
  • buffer_size is the maximum number of bytes in every output buffer.
  • record_overhead is the number of bytes added for each emitted fragment.
  • Return a list of buffers. Each buffer is a list of fragments represented as [message_index, offset, length].
  • offset is the payload offset within the original message, and length is the fragment payload length.
  • Fragment order must match message order. Empty messages produce no fragments.
  • Every fragment consumes record_overhead + length bytes, and each returned buffer must stay within buffer_size.

Use a greedy strategy: fill the current buffer with the largest possible next fragment. Start a new buffer when the next fragment cannot fit.

Constraints

  • 0 <= len(messages) <= 10^5
  • 0 <= messages[i] <= 10^12
  • 1 <= record_overhead < buffer_size <= 10^12
  • Message order must be preserved
  • Every non-empty fragment has positive payload length

Function Signature

def pack_dtls_messages(messages, buffer_size, record_overhead):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output