I remember this type of lexical analysis problem being in my compilers class. That said I have never used a stack in real coding but I can see various places where O(1) read/write to latest element can be useful. This problem from codility basically asks you to check if a String is properly nested.
// you can also use imports, for example: // import java.util.*; import java.util.Stack; // you can write to stdout for debugging purposes, e.g. // System.out.println("this is a debug message");