[Java] Implement a LRU cache

Here is an interesting and quite simple exercise: implement a LRU cache in Java.

When thinking about the problem, it appears immediately that we cannot do away with a single "basic" data structure, so we must combine at least two of them in a custom class. We use a HashMap for fast object retrieval and a custom doubly linked list to keep track of the objects access order, from MRU to LRU.

In this solution we have the put and get operations working in O(1) and we use additional O(N) space where N is the max cache size.

You can check the implementation on my Gist along with the definition of the ListNode class and some test cases.


Convert Ghostscript to PDF

To convert a Ghostscript file to PDF it's quite useful to have the utility ps2pdf installed on your system; it will come as part of the Ghostscript installation.

Then, make sure you have the gs command on the path and run either of these scripts depending on the PDF version you want to generate (note that the official documentation will be constantly updated, this page not!) :

  • ps2pdf12 produces PDF 1.2 output (Acrobat 3-and-later compatible).
  • ps2pdf13 produces PDF 1.3 output (Acrobat 4-and-later compatible).
  • ps2pdf14 produces PDF 1.4 output (Acrobat 5-and-later compatible).
  • ps2pdf per se currently produces PDF 1.4 output. However, this may change in the future. If you care about compatibility with a specific output level,use the -dCompatibilityLevel=1.x switch in the command line, or one of the specific version aliases ps2pdf12, ps2pdf13, or ps2pdf14.
Another thing to be aware of is that you might receive some errors while generating the PDF such as /invalidfileaccess in /findfont 

This might mean that you are missing a specific font installed on your system, but if that is not the case, maybe the font or some other file is not accessible due to permissions restrictions. You can try by running the utility again and adding -dnosafer as option.

[Sheet music] Leonard Cohen - Hallelujah

You cannot feel anything but peace after hearing this. Download it here