This should implement a stack that has a \$O(1)\$ getMin method. I used an auxiliary stack. Please provide comments on my solution.import org.junit.Test;public class Solution {  @Test  public void testEmptyStack(){    StackMin<Integer> s = new StackMin<Integer>();    System.out.println(----------------------);    System.out.println(Empty stack);    System.out.println(s.getMin());  }  @Test  public void testRandomStack(){    System.out.println(----------------------);    StackMin<Integer> s = new StackMin<Integer>();    s.push(2);    s.push(-1);    s.push(0);    s.push(7);    s.push(22);    System.out.println(Random stack);    s.printStack();    System.out.println(Min element);    System.out.println(s.getMin());  }  @Test  public void testEqualities(){    System.out.println(----------------------);    StackMin<Integer> s = new StackMin<Integer>();    s.push(2);    s.push(-1);    s.push(0);    s.push(0);    s.push(0);    s.push(7);    s.push(22);    s.push(-1);    s.push(-1);    s.push(33);    System.out.println(Stack with equalities);    s.printStack();    System.out.println(Min element);    System.out.println(s.getMin());  }  public static void main(String[] args) {    Solution e = new Solution();    e.testEmptyStack();    e.testRandomStack();    e.testEqualities();  }}class NodeV2<T> {  private final T data;  private final NodeV2<T> next;  public NodeV2(T d, NodeV2<T> n) {    data = d;    next = n;  }  public NodeV2(T d) {    this(d, null);  }  public T getData() {    return data;  }  public NodeV2<T> getNext() {    return next;  }}class StackMin<T extends Comparable<T>> extends StackV2<T> {  StackV2<T> auxiliaryStack = new StackV2<T>();  void push(T element) {    if (auxiliaryStack.isEmpty() || element.compareTo(auxiliaryStack.top.getData()) <= 0) {      auxiliaryStack.push(element);    }    super.push(element);  }  T pop() {    if (this.peek().compareTo(auxiliaryStack.peek()) <= 0) {      auxiliaryStack.pop();    }    return super.pop();  }  public T getMin() {    return auxiliaryStack.peek();  }}class StackV2<T extends Comparable<T>> {  NodeV2<T> top;  void push(T element) {    NodeV2<T> t = new NodeV2<T>(element, top);    top = t;  }  T pop() {    if (top != null) {      T item = top.getData();      top = top.getNext();      return item;    }    return null;  }  T peek() {    return top == null ? null : top.getData();  }  boolean isEmpty() {    return top == null ? true : false;  }  public void printStack() {    NodeV2<T> current = top;    while (current != null) {      System.out.println(current.getData());      current = current.getNext();    }  }}
Stack with getMin method
java;algorithm;linked list;stack
Yay, you use tests!Darn it, you don't use tests.Confusing? Allow me to explain. While I totally approve of you using tests, there is a lot more to it than simply putting the @Test annotation above them.A test is comprised of 3 aspects: Arrange, Act and Assert. Typically a test (whether it's unit, integration, functional, end-to-end, etc) will have these aspects in explicit or implicit form. Personally I like to explicitly separate them with comments (// Arrange).Why do I mention this? Note the last aspect of AAA: Assert. This tells us that, as the last step in our test, we will verify the result we have and assert that it is what we expected.Doing this allows us to run the tests and automatically compare the result of each test with the expected outcome. In your situation you have to manually verify that each test works as expected; can you see yourself doing that when you have 300 tests instead of 3?This brings us to another point: all modern IDE's have integrated test runners. If you have tests, you can just run these using that instead of having to use a main() method as entrypoint for your tests.By adapting these changes you also won't need those println's anymore which takes away focus from your actual content.Junit documentationJunit tutorial