PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Microsoft

Assemble async stream reads into objects

Last updated: Mar 29, 2026

Quick Overview

This question evaluates a developer's skill in asynchronous stream I/O, buffer management, and binary parsing in C#, including correctly handling partial reads and EOF/error conditions when assembling length-prefixed records.

  • hard
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Assemble async stream reads into objects

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

You are implementing a parser in **C#** that reads a binary stream containing a sequence of records. An interviewer provides these helper functions (you may not change their behavior): ```csharp // Reads up to n bytes from the underlying stream asynchronously. // Returns an empty array when the stream is at EOF. Task<byte[]> ReadAsync(int n); // Decodes exactly 4 bytes into an Int32 length. Task<int> DecodeInt(byte[] fourBytes); // Decodes a byte sequence into one object instance. Task<object> DecodeObject(byte[] bytes); ``` The stream format is: - Repeatedly: 1. Read **4 bytes** → decode as an `int L` (the length of the next object payload in bytes) 2. Read **L bytes** → decode into an `object` - Continue until EOF is reached cleanly. ### Task Implement `Task<List<object>> ReadAllObjectsAsync()` that reads the entire stream and returns the decoded objects in order. ### Requirements - Must handle **partial reads** (i.e., `ReadAsync(n)` may return fewer than `n` bytes). - If EOF occurs in the middle of a record (e.g., only 2 of 4 length bytes are available, or payload is truncated), treat it as an error (you may throw). - Assume `L >= 0`. You may define reasonable upper bounds/guards if you want.

Quick Answer: This question evaluates a developer's skill in asynchronous stream I/O, buffer management, and binary parsing in C#, including correctly handling partial reads and EOF/error conditions when assembling length-prefixed records.

Related Interview Questions

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)
Microsoft logo
Microsoft
Feb 11, 2026, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
4
0
Loading...

You are implementing a parser in C# that reads a binary stream containing a sequence of records.

An interviewer provides these helper functions (you may not change their behavior):

// Reads up to n bytes from the underlying stream asynchronously.
// Returns an empty array when the stream is at EOF.
Task<byte[]> ReadAsync(int n);

// Decodes exactly 4 bytes into an Int32 length.
Task<int> DecodeInt(byte[] fourBytes);

// Decodes a byte sequence into one object instance.
Task<object> DecodeObject(byte[] bytes);

The stream format is:

  • Repeatedly:
    1. Read 4 bytes → decode as an int L (the length of the next object payload in bytes)
    2. Read L bytes → decode into an object
  • Continue until EOF is reached cleanly.

Task

Implement Task<List<object>> ReadAllObjectsAsync() that reads the entire stream and returns the decoded objects in order.

Requirements

  • Must handle partial reads (i.e., ReadAsync(n) may return fewer than n bytes).
  • If EOF occurs in the middle of a record (e.g., only 2 of 4 length bytes are available, or payload is truncated), treat it as an error (you may throw).
  • Assume L >= 0 . You may define reasonable upper bounds/guards if you want.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Microsoft•More Software Engineer•Microsoft Software Engineer•Microsoft 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.