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 7582059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:20:46+00:00 2026-05-30T18:20:46+00:00

This is a interview Question which was asked and wanted to find an efficient

  • 0

This is a interview Question which was asked and wanted to find an efficient solution.

Problem

Consider a intersection of four roads as shown in the diagram. Each road is defined to have a direction. How would you solve the problem so as to improve the traffic condition and avoid deadlocks.

  1. the intersection is divided into four quadrants [shown in yellow].
  2. Cars enter the intersection at random from the four direction [0,1,2,3]
  3. At the intersection each car can make a single move. the three possible moves are
    1. Go left
    2. Go straight
    3. Go right

e.g :

if a car enters from direction 2 and wants to take a left turn. it should pass through
quandrant 2 , quandrant 1 and finally quandrant 0

enter image description here

Semi complete solution

Each quadrant marked in yellow has a semaphore associated with it.
What I imagined was a 2 phase protocol wherein each car would

  1. get list of quadrants it will pass through
  2. lock each of the quadrants starting from the last quadrant
    1. In the above example the car from direction 2 would lock quadrant 0, quadrant 1 and then quadrant 2.
  3. Move through the intersection
  4. Release the locks acquired.
    1. The locks are released in the same order. quadrant 0, quandrant 1 and the quandrant 2

However the above solution is less than optimal as it results in a deadlock.

My Question is

  1. What other /better way can this be achieved?
  2. Can I implement this using a semaphore in C?
    1. If not what synchronization method should I be looking at?

Updates

  1. I want a solution which allows multiple cars can enter the intersection and still avoid deadlock as well as collision.Having a single lock on the entire intersection would be less than optimized.
  • 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-05-30T18:20:48+00:00Added an answer on May 30, 2026 at 6:20 pm

    I was able to implement a solution in C using the following logic (The actual implementation used semaphores for each of the quadrant).

    The solution I mentioned in the question was partially correct however I was experiencing deadlock due to the matter in which locks were made.
    I was able to solve the issue by assigning priority to the locks.
    The locks are gained by locking highest priority lock first.
    Following is a pseudo-code algorithm used by each of the car:

    Assumptions:

    1. Each quadrant has a lock associated with it.
    2. Each lock has an associated priority. The priorities are as follows

    Priority(Lock_0) < Priority(Lock_1) < Priority(Lock_2) < Priority(Lock_3)
    *where Lock_x is the lock for Quadrant x

    Algorithm

    1. Each car gets list of quadrants it will pass through

    2. Lock each of the quadrants starting from the quadrant with highest priority.

      e.g: In the above example the car from direction 2 taking a left turn would lock quadrant 2, quadrant 0 and then quadrant 1.

    3. Move through the intersection

    4. Release the locks acquired.

      e.g: In the above example the locks are released in the same order. quadrant 2, quandrant 1 and the quandrant 0

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

Sidebar

Related Questions

This question was recently asked to me in an interview for which i went
This is the interview question asked. Which is the most important factor while drawing
I was asked this question during an interview. What they wanted to know was
Got asked this interesting interview question today. Explain in detail the process by which
I was asked this question in interview. Which of the following is better to
This is a question I was asked at my interview recently: Which 'Random' object(s)
Hii , I was asked this question in a recent interview for which i
This is an interview question asked a month ago.... Do session use cookies? If
This question was asked at interview. Say I have a contract. [ServiceContract] public interface
This question was asked to me in an interview. Suppose char *p=malloc(n) assigns more

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.