Implement Queue using fixed size array Announcing the arrival of Valued Associate #679: Cesar...

If a contract sometimes uses the wrong name, is it still valid?

What's the meaning of 間時肆拾貳 at a car parking sign

How to find all the available tools in mac terminal?

Identify plant with long narrow paired leaves and reddish stems

How to tell that you are a giant?

Can I cast Passwall to drop an enemy into a 20-foot pit?

What is the meaning of the new sigil in Game of Thrones Season 8 intro?

What to do with chalk when deepwater soloing?

How do I keep my slimes from escaping their pens?

What LEGO pieces have "real-world" functionality?

Can a USB port passively 'listen only'?

Why light coming from distant stars is not discreet?

Output the ŋarâþ crîþ alphabet song without using (m)any letters

ListPlot join points by nearest neighbor rather than order

What is a non-alternating simple group with big order, but relatively few conjugacy classes?

Fundamental Solution of the Pell Equation

At the end of Thor: Ragnarok why don't the Asgardians turn and head for the Bifrost as per their original plan?

When a candle burns, why does the top of wick glow if bottom of flame is hottest?

What is the logic behind the Maharil's explanation of why we don't say שעשה ניסים on Pesach?

Why do people hide their license plates in the EU?

How come Sam didn't become Lord of Horn Hill?

How to align text above triangle figure

Is the Standard Deduction better than Itemized when both are the same amount?

Why are there no cargo aircraft with "flying wing" design?



Implement Queue using fixed size array



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Implement data structure overflow queueFind elements occurring even number of times in an integer arrayFind the dominator in an array of integersDetect the missing number in a randomly-sorted arrayAdd 1 to an integer represented as an array of digitsMaximum Sum of Non-Adjacent ElementsSolving this Python implementation for Stock GainsCodility: MaxZeroProduct - complexity issuesTwo-sum solution in JavaScriptMinMax Problem Implementation





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0












$begingroup$


I came across below interview question and I am working on it:




Build a queue class with the enqueue and dequeue methods. The queue
can store an UNLIMITED number of elements but you are limited to
using arrays that can store up to 5 elements max.




Here is what I was able to come up with. Is this the right way to do it in the interview or is there any better way we should implement in the interview?



class Solution {  
private final List<List<Integer>> array;

public Solution() {
this.array = new ArrayList<>();
}

public void enqueue(int value) {
if(array.isEmpty()) {
List<Integer> arr = new ArrayList<>();
arr.add(value);
array.add(arr);
return;
}
if(array.get(array.size() - 1).size() != 5) {
array.get(array.size() - 1).add(value);
return;
}
List<Integer> arr = new ArrayList<>();
arr.add(value);
array.add(arr);
return;
}

public int dequeue() {
if(array.isEmpty()) {
return -1;
}
for(List<Integer> l : array) {
for(int i=0; i<l.size(); i++) {
return l.remove(i);
}
}
return -1;
}
}








share









$endgroup$



















    0












    $begingroup$


    I came across below interview question and I am working on it:




    Build a queue class with the enqueue and dequeue methods. The queue
    can store an UNLIMITED number of elements but you are limited to
    using arrays that can store up to 5 elements max.




    Here is what I was able to come up with. Is this the right way to do it in the interview or is there any better way we should implement in the interview?



    class Solution {  
    private final List<List<Integer>> array;

    public Solution() {
    this.array = new ArrayList<>();
    }

    public void enqueue(int value) {
    if(array.isEmpty()) {
    List<Integer> arr = new ArrayList<>();
    arr.add(value);
    array.add(arr);
    return;
    }
    if(array.get(array.size() - 1).size() != 5) {
    array.get(array.size() - 1).add(value);
    return;
    }
    List<Integer> arr = new ArrayList<>();
    arr.add(value);
    array.add(arr);
    return;
    }

    public int dequeue() {
    if(array.isEmpty()) {
    return -1;
    }
    for(List<Integer> l : array) {
    for(int i=0; i<l.size(); i++) {
    return l.remove(i);
    }
    }
    return -1;
    }
    }








    share









    $endgroup$















      0












      0








      0





      $begingroup$


      I came across below interview question and I am working on it:




      Build a queue class with the enqueue and dequeue methods. The queue
      can store an UNLIMITED number of elements but you are limited to
      using arrays that can store up to 5 elements max.




      Here is what I was able to come up with. Is this the right way to do it in the interview or is there any better way we should implement in the interview?



      class Solution {  
      private final List<List<Integer>> array;

      public Solution() {
      this.array = new ArrayList<>();
      }

      public void enqueue(int value) {
      if(array.isEmpty()) {
      List<Integer> arr = new ArrayList<>();
      arr.add(value);
      array.add(arr);
      return;
      }
      if(array.get(array.size() - 1).size() != 5) {
      array.get(array.size() - 1).add(value);
      return;
      }
      List<Integer> arr = new ArrayList<>();
      arr.add(value);
      array.add(arr);
      return;
      }

      public int dequeue() {
      if(array.isEmpty()) {
      return -1;
      }
      for(List<Integer> l : array) {
      for(int i=0; i<l.size(); i++) {
      return l.remove(i);
      }
      }
      return -1;
      }
      }








      share









      $endgroup$




      I came across below interview question and I am working on it:




      Build a queue class with the enqueue and dequeue methods. The queue
      can store an UNLIMITED number of elements but you are limited to
      using arrays that can store up to 5 elements max.




      Here is what I was able to come up with. Is this the right way to do it in the interview or is there any better way we should implement in the interview?



      class Solution {  
      private final List<List<Integer>> array;

      public Solution() {
      this.array = new ArrayList<>();
      }

      public void enqueue(int value) {
      if(array.isEmpty()) {
      List<Integer> arr = new ArrayList<>();
      arr.add(value);
      array.add(arr);
      return;
      }
      if(array.get(array.size() - 1).size() != 5) {
      array.get(array.size() - 1).add(value);
      return;
      }
      List<Integer> arr = new ArrayList<>();
      arr.add(value);
      array.add(arr);
      return;
      }

      public int dequeue() {
      if(array.isEmpty()) {
      return -1;
      }
      for(List<Integer> l : array) {
      for(int i=0; i<l.size(); i++) {
      return l.remove(i);
      }
      }
      return -1;
      }
      }






      java algorithm interview-questions





      share












      share










      share



      share










      asked 3 mins ago









      user5447339user5447339

      161110




      161110






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217592%2fimplement-queue-using-fixed-size-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217592%2fimplement-queue-using-fixed-size-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          is 'sed' thread safeWhat should someone know about using Python scripts in the shell?Nexenta bash script uses...

          How do i solve the “ No module named 'mlxtend' ” issue on Jupyter?

          Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...