Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8806775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:12:35+00:00 2026-06-14T02:12:35+00:00

I was asked the following question in an interview: There is a fixed size

  • 0

I was asked the following question in an interview:

There is a fixed size queue of tasks. Threads want to enqueue task. If the queue is full they should wait. The threads order should remain: if thread1 came with task1 and after that thread2 came with task2, task1 should enter the queue before task2.

Other threads want to dequeue tasks and execute it. If the queue is empty, they should wait, and also their order should remain: If t3 came before t4, t3 should enqueue task before t4.

How to achieve this (in pseudo-code)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:12:37+00:00Added an answer on June 14, 2026 at 2:12 am
    1. Simple solution
      In .NET 4.0 were introduced namespace System.Collections.Concurrent, class from it are working quite right – I couldn’t achieve some error from them.
      ConcurrentQueue and BlockingQueue are the place to start your research.
      But I think your question is not about the standart solution – that’s bad answer on the interview, so:
    2. Solution based on Jeffrey Richter’s book information:
      Base code (C#):

      internal sealed class SynchronizedQueue<T> {
          private readonly Object m_lock = new Object();
          private readonly Queue<T> m_queue = new Queue<T>();
      
          public void Enqueue(T item) {
              Monitor.Enter(m_lock);
              // After enqueuing an item, wake up any/all waiters
              m_queue.Enqueue(item);
              Monitor.PulseAll(m_lock);
              Monitor.Exit(m_lock);
          }
      
          public T Dequeue() {
              Monitor.Enter(m_lock);
              // Loop while the queue is empty (the condition)
              while (m_queue.Count == 0)
                  Monitor.Wait(m_lock);
              // Dequeue an item from the queue and return it for processing
              T item = m_queue.Dequeue();
              Monitor.Exit(m_lock);
              return item;
          }
      }
      

      This class is a thread-safe, but still doesn’t check the order – and here is a many ways to implement that. From the same book:

      ConcurrentQueue and ConcurrentStack are lock-free; these both internally use
      Interlocked methods to manipulate the collection.

      So, you must remove Monitor class usage, and provide check for your thread to being next one to enqueue item.
      This can be done by maintaining the number of current adders and current queue length in the private field. You should make this fields volatile.
      You should use Interlocked.Exchange to get your current adders and Interlocked.Read to get your current queue length.
      After that, you have unique number for your thread – current length + current adders. Use SpinWait class to spin around while current length will not became equal to your number, after that enqueue item, and leave the Enqueue method.

    I strongly recommend you to study this book chapters about multithreading and locks – you’ll be much more prepared for this type questions in your life. Also try to search similar questions here. For example:

    Creating a blocking Queue<T> in .NET?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was asked the following question in an interview: Is there any way in
I recently attended an interview and was asked the following question. There are two
Recently I was asked the following question at interview. What are the different way
I got asked the following question in an interview while talking about concurrency in
A friend of mine was asked the following question today at interview for the
The following question was asked in one of my interview (few years back) What
I was asked in an interview this question so I answered with the following:
Recently I was asked the following interview question: You have two sets of numbers
I was asked the following question during phone interview I had: Given the following
Recently in a job interview I was asked this following question (for Java): Given:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.