JavaRush /Java Blog /Random EN /Coffee break #151. Implementation of a Queue in Java. 10 ...

Coffee break #151. Implementation of a Queue in Java. 10 Must-Have Questions for Any Job Interview for a Java Backend Developer Position

Published in the Random EN group

10 Must-Have Questions for Any Job Interview for a Java Backend Developer Position

Source: Medium The author of the article shares his personal experience of passing several interviews for the position of Java Backend Developer. Coffee break #151.  Implementation of a Queue in Java.  10 must-have questions for any interview for a Java Backend Developer position - 1Interviews are often difficult. Therefore, it would be a good idea to know the questions before taking the interview. Since I've been through a number of interviews over the last couple of months, I can now share with you some of the questions that I've encountered most often. I applied for a position as a backend Java developer with 1-3 years of experience.

1. Java and its features

The interviewer will usually ask about the latest version of Java you are using and its features. I have mostly used Java 8 and have been asked questions about API streams, lambda expressions, functional interface and so on. The interviewer typically asked about these features and applications based on them.

2. Internal working of HashMap

Most of the interviews I attended had this question. After explaining how it works, some asked about the default HashMap size, others asked more about Java collections.

3. Errors and exceptions

The difference between Error and Exception is a very common question asked in interviews. Also asked what are checked and unchecked exceptions.

4. Multithreading

Multithreading is a very important concept for interviews, usually questions are asked about Thread, Runnable classes, differences between them and use cases of both. I also advise you to study the Executor framework.

5. Strings and immutability

Most interviewers asked about immutability in the string class, and then sometimes asked about string builder and string buffer (if you said they were mutable alternate strings). Sometimes they asked how to create an immutable class.

6. Garbage collection

Garbage collection in Java is a very important topic to prepare for interviews. Questions are sometimes asked about the garbage collection lifecycle and the different methods of garbage collection.

7. Comparator and Comparable in Java

Commonly asked questions are when to use Comparator and Comparable in Java and what is the difference between them.

8. Java Design Patterns

When it comes to Java design patterns, questions usually start with the different patterns used in the current project. Sometimes questions are asked about when to use each of them. Also asked about creating an immutable class in Java.

9. Final, Finally and Finalize

The difference between the keywords final, finally and finalize is a very common question asked in interviews. As an additional question, they ask about options for using each keyword.

10. Serialization and Deserialization in Java

Serialization and deserialization is a very important topic to prepare for interviews. Usually asked about use cases, sometimes asked about hibernation and JPA. Hopefully, if you are facing an interview for a Java Server Side Developer position soon, preparing the questions mentioned here will help you.

Implementing a Queue in Java

Source: Faun.pub Thanks to this publication, you will learn how to implement a Queue in Java. Coffee break #151.  Implementation of a Queue in Java.  10 must-have questions for any interview for a Java Backend Developer position - 2

What is a queue?

A Queue is a linear data structure in the form of a sequence of access to elements according to the “first in, first out” principle. This means that the element that is inserted first will be removed first. That is, elements are removed in the order in which they were inserted. The queue consists of two parts: Front (front, where elements are removed) and Back (back, where elements are inserted). Common Queue Operations: The following operations are commonly used in a queue:
  • Enqueue - Adds an element from the end of the queue.
  • Dequeue - Removes an element from the head of the queue.
  • Front/Peek - returns the value of the element in front of the queue, without excluding (removing) the element from the queue.
  • IsEmpty - checks if the queue is empty.
  • IsFull - checks if the queue is full.
  • Display - Prints all items in the queue.

Code implementation:

public class Example {
public static void main(String[] args) {
    Queue myQueue = new Queue();
    myQueue.enQueue(3);
    myQueue.enQueue(2);
    myQueue.enQueue(1);
    myQueue.display();
    myQueue.deQueue();
    myQueue.peak();
}
}
class Queue {
  int queueLength = 3;
  int items[] = new int[queueLength];
  int front = -1;
  int back = -1;
  boolean isFull(){
      if(back == queueLength - 1){
          return true;
      } else {
          return false;
      }
  }
  boolean isEmpty(){
      if(front == -1 && back == -1){
          return true;
      } else {
          return false;
      }
  }
   void enQueue(int itemValue) {
      if(isFull()){
          System.out.println("Queue is full");
      } else if(front == -1 && back == -1){
          front = back = 0;
          items[back] = itemValue;
      } else{
          back++;
          items[back] = itemValue;
      }
  }
   void deQueue(){
      if(isEmpty()){
          System.out.println("Queue is empty. Nothing to dequeue");
      } else if (front == back){
          front = back = -1;
      } else {
          front++;
      }
  }
  void display(){
      int i;
      if(isEmpty()){
          System.out.println("Queue is empty");
      } else {
          for(i = front; i <= back; i++){
              System.out.println(items[i]);
          }
      }
  }
  void peak(){
      System.out.println("Front value is: " + items[front]);
  }
}

Explanation:

  • First we created our variables and their parameters. We use 3 as the maximum number of elements that can be queued in the array. We set the initial index of the front (Front) and back (Back) to -1.

  • Next we will define the functionality of isEmpty and isFull .

  • The isEmpty() method is quite simple, for the isFull() method our maximum number of elements allowed in an array is 3, but three elements in the array are not denoted by index 3 but by 2 since the first index is 0. So the maximum length is minus 1 gives us index 2, which is the third cell in the array. When all cells are queued with a value up to the third cell, the array is full.

  • enQueue - if the array is full, we get a message that it is full. If the Front and Back parts are -1, then the element is assigned to the first cell at index 0 - otherwise the value is inserted and the back position is incremented.

  • deQueue - if the array is empty, we receive the corresponding message. If the Front part meets the Back part, then we reset their index back to -1. If the last two conditions do not apply, then Front is increased.

  • display - if the array is not empty, we iterate and print all the elements.

  • peak - This simply prints the value of the leading element.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION