Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am practicing LinkedList problems. How do I make it production level code as is mostly expected in a coding interview? Any other Suggestions?

package LinkedList;

/**
 * @author Amogh Antarkar 
 * @email [email protected]
 */
import java.util.*;

public class LinkedList{

    public static void main(String[]args){

        switchCase();
        System.out.println("Successful completion of the LinkedList program");
    }

    /* Note: Here a singleton pattern has been used for the Scanner in 
     * as only one instance is created during the entire implementation
     */

    public static void switchCase(){
        int cont;
        Scanner in = new Scanner(System.in);
        LinkedListNode list = new LinkedListNode();

        do{
            System.out.println("Enter the option you want to run");
            System.out.println("1. remove duplicates\n2. Print List\n 3. Make List\n 4.kth to last element");
            int option = in.nextInt();
            switch(option){
            case 1: // remove duplicates
                System.out.println("option chosen:"+option);
                list.removeDuplicates();
                break;

            case 2:
                list.printList();
                break;

            case 3: 
                list.makeList(list,in);
                break;

            case 4: 
                System.out.println(" Enter k");
                int j = list.kthLastElement(list,in.nextInt());
                break;

            default:
                break;
            }
            System.out.println("Do you want to continue ?");
            cont = in.nextInt();

        }while(cont == 1);

        in.close();
    }
}

class LinkedListNode{

    private int data; 
    private LinkedListNode next = null;

    public LinkedListNode(int d){
        data = d;
        next = null;
    }

    public LinkedListNode() {
    }

    // appends data to the List
    void appendData(int d){
        LinkedListNode end = new LinkedListNode(d);
        LinkedListNode n = this;
        while(n.next != null){
            n = n.next;
        }
        n.next = end;
    }

    // creates the LinkedList taking the input
    void makeList(LinkedListNode list, Scanner in){

        System.out.println("Enter the list");
        int len = in.nextInt();
        while(len-- > 0){
            list.appendData(in.nextInt());
        }
    }

    //implementing the recursive solution
    int kthLastElement(LinkedListNode head,int k){

        if(head == null){
            return 0;
        }
        else{
            int i = kthLastElement(head.next,k)+1;
            if(i==k)
                System.out.println(head.data);
            return i;
        }


    }

    // print the entire LinkedList O(n) time
    void printList(){
        LinkedListNode  n = this;
        while(n.next!= null){
            n = n.next;
            System.out.print(n.data+" -> ");
        }
        System.out.print("NULL\n");
    }

    // remove duplicates from the linkedList using Hashtable method
    void removeDuplicates(){

        Hashtable<Integer, Boolean> table = new Hashtable<Integer, Boolean>();
        LinkedListNode current = this, prev = null;

        while(current != null){
            if(table.containsKey(current.data)){
                prev.next = current.next;
            }else{
                table.put(current.data,true);
                prev = current;
            }
            current = current.next;
        }
    }
}
share|improve this question
    
Couple minor additions to hjk's answer: You are implementing a singy-linked list, so your name should reflect that. One of the big things in Java are iterators. A few extras you can try to work with are : Can you add the possibility for your list to return an iterators? Can it return a ListIterator? Can you detect ConcurrentModificationExceptions? –  Diego Martinoia Aug 3 at 14:39

1 Answer 1

Names

package LinkedList

By Java's naming convention, package names are in lowercase and follows a 'reversed Internet domain name' prefix. Therefore, LinkedList is not a good package name, but something like com.antarkar.amough may be more suitable.

public class LinkedList

It is usually not recommended to use class names that conflict with those from the standard API, as it may cause confusion at best and compilation errors at worst. Since you are indeed doing a linked list implementation, I don't suppose there's a simpler way around this, just bear this in mind in the future for 'production level code'...

Separation of concerns

The biggest issue about this class, going by 'production level code' and 'coding interview' standards, is that you are mixing I/O operations (i.e. user input and display) with the data structure implementation. These two should stay separate for two simple reasons, if I may classify them under the standards you requested:

  1. Production level code

    When codes get into 'production' (depending on your definition), you'll want it to do one thing, do it well, and do it in as minimal as possible. If it's a data structure, it shouldn't know how to interact with System.in and System.out. If logging is required, it will use a (preferably widely-adopted and well-tested) logging framework to do so. Lesser lines of code tend to result in less programming bugs introduced. Lesser instructions (at the JVM level) also translate to more efficient memory usage, too.

  2. Coding interview

    Intertwining two unrelated features into a single class may indicate a lack of understanding of OOP concepts. What happens if a GUI-based demonstration is required? Also, how will you unit test your code when the class is interfacing directing with System.in and System.out, when it should be done through proper method arguments and (possibly) String outputs?

I'll recommending browsing through the number of linked list implementations in Java we have here on CR, to compare-and-contrast your implementations with the rest.

Modern code

Without even going into Java 8 features, features like try-with-resources should already be adopted around your Scanner so that you do not need an explicit call to close() at the end. Java 7 already has generic type inference aka the 'diamond operator', so you can remove the unnecessary types. Hashtable is a legacy class that is retrofitted to become part of the Collections framework, so you really ought to be using HashMap.

All these pointers matter if this is for a coding interview, as you'll want to show that you are reasonably kept up to date with the newer developments in your programming language of choice.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.