LinkedList – Nth element

Consider a singly linked list. What is the optimal way to traverse to an Nth element from the last element of that singly linked list. Any algorithms or logic to achieve in optimal way?

Naive soln :

  • First find the total number of nodes, by iterating the entire list ..
  • Now subtract the nth last node from the total number
  • Now again iterate from the head node to the nth node…

But it require two complete iteration..

Optimized soln :

  • Create two pointers and point those pointers to head node..
  • Iterate first pointer to nth node..
  • Now, iterate both first node pointer and second node pointer, until first node pointer reaches the end of the list .. Now second pointer will be pointing to the nth node from the last node.. Continue Reading »

How to create a dynamic class “on the fly” :

com.sun.tools.javac.Main class has two methods which help us to invoke the compiler from a program. It will behave like javac command.
public static int compile(String []args);
public static int compile(String []args , PrintWriter out);

args-> command line arguments that would be normally be passed on the javac program.
out-> indicates where the compiler’s output is directed.
return (int) -> exit value

I would like to share a simple program which will create a java file on the fly (ie at Runtime) and it will be compiled using compile method provided by this class com.sun.tools.javac.Main.Once its  been compiled,using reflection apis we will invoke the method in the class(which is created at Runtime).. Continue Reading »

Java Disassemble

Java -Dis@$$emble

This material -> It would be helpful for java interview for sure (for Product company 🙂 ) .. Its bit lengthy, I will try to modify this material.. Even I hate to read big paragraph, first time I took some notes and created a simple doc..  I am glad to share this with you all..

Till now, I didn’t concern about the details of what happened to the source code when it’s run through the compiler. I would like to share my findings (behind the scene details).

Javap utility is used to disassemble java bytecode (ie binary format into readable format) .This utility is included in Jdk . Continue Reading »



    *Only one instance of class will be created

    Consider this class
    public class Singleton
    Continue Reading »

Java Interview Part Two

Java Interview:

Prod Comp 2 :[2+yr][Programmer][Dec-2009]

I am glad to say that, I have cleared this interview(I got confirmation from this company).

Apart from telephonic interview, never messed up anywhere,
Be cool n relax, that’s d mantra…. Be confident, don’t be over confident..

Continue Reading »

Java Interview Part One

Java interview questions:

I would like to share my interview exp with 2 product company situated in India..I am glad to say that, I have cleared both the interviews(I got confirmation from both company).. I will share those interview questions, so that it would be helpful for our preparation. Even though I have cleared the interview, I am looking forward to work in a project where it will involve some innovation.

Certifications help me a lot… Those who love java, I would request them to take Sun certification..It will help us to understand the concepts as well as its Pros & Cons.

Soon I will update the answers; at least I will give the pointers for the solutions…

Prod Comp 1:[2+ yr][Programmer][Jan-2010] Continue Reading »


Java Virtual Machine:

Main purpose : To run the compiled Java code across different network with out any modification.

Jvm doesn’t know abt Java language, it accepts only particular format (.class).A class file contains Java virtual machine instructions (bytecode) and a symbol table  as well as ancillary information.It can also be used to run other languages other than java.For example , Ada source code can be compiled to java bytecode and it can be executed by JVM.JVM executes .class files ,by using interpreter to emulate the instruction set or by using Just-in-Time compiler(JIT). JIT will not interpret and its used in most JVM to achieve greater speed.

JIT will convert bytecode into native machine code , thus it will improve the performance as well as it will caches the results of translating blocks (so it avoid reevaluating each line or operand each time it is met)..

How JVM Works :
-> Class will be loaded using ClassLoader(it will find the binary representation of classes).. Continue Reading »