LetMyPeopleCode.com

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

Menu
  • About Greg
  • About LMPC
  • 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

  • My first commute into Seattle since COVID
  • Lenovo P12 Review: A Second Screen – With Caveats
  • Defective Quidel QuickVue COVID Tests
  • The Great American Browser Hunt
  • Chrome’s Usability Crime

Archives

  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • November 2021
  • May 2021
  • April 2020
  • March 2020

Categories

  • Apps
  • Games
  • Hardware
  • JavaScript
  • Mobile
  • Productivity
  • Programming
  • Society & Culture
  • Teaching Code
  • Tech Career
  • Uncategorized
  • WebTech
  • Writing

Tag Cloud

browser hunt code sample commute CX game demo hardware review health interview questions j job hunt diary Mac meta-coding OneNote review Windows

©2022 LetMyPeopleCode.com | Theme by SuperbThemes