LetMyPeopleCode.com

A blog about software, schmaltz, and monkey-patches for the soul

Menu
  • About Greg Bulmash
  • Greg Talks Tech
  • Privacy Policy
Menu
"The Coding Interview"

The Coding Interview: Create A Circular Queue

Posted on January 28, 2022 by Greg Bulmash

This series answers coding interview questions from the Coding Interview Prep collection at freeCodeCamp. You can find more with the interview questions tag.

This is a data structure problem.

A circular queue allows you to specify a size for easier memory management. It’s initialized with a set number of slots. Each write operation fills a slot, each read operation empties a slot. And the slots get used in a round-robin order. If there are four slots, it goes 0, 1, 2, 3, 4, 0, 1, 2... It loops around to the beginning when it hits the end.

The rules of this exercise were that data couldn’t be overwritten, so if the queue is full, data must be read to make room. If there’s no data to read or no free slots to fill, the en/dequeue operation should return null.

Solution explained

The exercise only permitted/required writing the enqueue and dequeue functions. I actually had to run it with some console.log statements to see what format the incoming data would use. It was just numbers.

It also helped to think of this as a buffer for streaming, like their description text suggested.

The functions were really very similar.
– Check to see if the space was open (enqueue) or full (dequeue).
– If so:
– Write or read the data as needed.
– If a read operation, purge the data in the space.
– Increment the write/read head, wrapping at the end.
– If not:
– return null

That passed the tests.

Solution

class CircularQueue {
  constructor(size) {

    this.queue = [];
    this.read = 0;
    this.write = 0;
    this.max = size - 1;

    while (size > 0) {
      this.queue.push(null);
      size--;
    }
  }

  print() {
    return this.queue;
  }

  enqueue(item) {
    if(this.queue[this.write] === null){
      this.queue[this.write] = item;
      this.write++;
      if(this.write > this.max) this.write = 0;
      return item;
    }
    return null;
  }

  dequeue() {
    if(this.queue[this.read] !== null){
      let item = this.queue[this.read];
      this.queue[this.read] = null;
      this.read++;
      if(this.read > this.max) this.read = 0;
      return item;
    }
    return null;
  }
}

 

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • Job Hunt Diary 2023: Verizon Is Passive-Aggressive?
  • Job Hunt Diary 2023: Eliciting Responses
  • Job Hunt Diary 2023: And so on, and so on…
  • Job Hunt Diary 2023: Freelancing and warning words
  • Job Hunt Diary 2023: Not all job fairs are created equal

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • September 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • November 2021
  • May 2021
  • April 2020
  • March 2020

Categories

  • Apps
  • Computing
  • Frameworks and Libraries
  • Games
  • Hacks
  • Hardware
  • JavaScript
  • Mac
  • Mobile
  • Other Art Projects
  • Productivity
  • Programming
  • Projects
  • Python
  • Society & Culture
  • Teaching Code
  • Tech Career
  • Uncategorized
  • WebTech
  • Writing

Tag Cloud

Adobe advertising bad ads browser hunt clown car code sample comcast commute copy writing CX design Developer Relations dev rel evernote game demo google drive hardware review health home office home studio internet interview questions j job hunt job hunt diary Mac magento meta-coding OneNote PEO productivity review speedtest t-mobile Windows xfinity

©2023 LetMyPeopleCode.com | Theme by SuperbThemes