Posts

Showing posts with the label Java

Problem Solving: Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node

Problem Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node EXAMPLE Input: the node ‘c’ from the linked list a->b->c->d->e Result: nothing is returned, but the new linked list looks like a->b->d->e Solution The question is a bit confusing. I first though its asking to remove the middle element of the list and I thought we have the full list. But later I think I got it. As per my understanding is we clear the node which we have access only and it will be somewhere in the middle. What we can do, we can migrate the data from next one to this one and set next to next of next. Code https://github.com/tapos/ctci/tree/master/src/p23

Problem Solving: Write code to remove duplicates from an unsorted linked list.

Problem Write code to remove duplicates from an unsorted linked list. Solution Two types of solution came to my mind 1) Use hashmap to check the duplicates and remove that as necessary. Time complexity for this solution is O(n) and Space Complexity O(n) 2) We also can check if by checking all previous entries for duplciate. In this case time complexity will be O(n^2) and Space Complexity O(1) Code https://github.com/tapos/ctci/tree/master/src/p21

Problem Solving: Circular Array

I was trying to get back to solve some problems to prepare an interview. I have started an easy problem on HackerRank - Circular Array Here is the details of problem and solution -  https://github.com/tapos/hackerrank/tree/master/src/circulararray

Balanced Parenthesis

Problem Description: For given 15 > n > 0, print all balanced parenthesis eg: For n = 1 () n = 2 ()() (()) n=3 ((())) (()()) (())() ()(()) ()()() Solutions : Solution is simple. Start with empty string  Add only valid parenthesis at the end  And continue until you reach to the length Code : public class BalancedParenthesis { public void printParenthesis(int n) { print("", n, 0, 0); } private void print(String strSoFar, int n, int opc, int cpc) { if(opc == n && cpc == n) { System.out.println(strSoFar); } if(opc < n) { print(strSoFar + "(", n, opc + 1, cpc); } if(cpc < opc) { print(strSoFar + ")", n, opc, cpc + 1); } } public static void main(String[] agrs) { BalancedParenthe...

Java Books

Some time I have been asked what are some good books for Java. Though I am not sure how to put things up but I will try to write my experience with various books.  My first Java learning was in 2nd year of my undergrad back in 2003. I have started with recommend book from my teacher - Java: The Complete Reference by Herbert Schildt. At that time I saw it as It has everything I need to know about Java and it was quite good for me to step into Java as I had experience in C. One of my friend bought Java How to Program by Deitel & Deitel. After looking at that a bit I found it very friendly for me as I was a beginner in Java. I liked the structuring and detailness.  Other than those two books, I have read few other Java books like  Head First Java (which does not have a new version now a days.) - I first came to Head First series from Head First Design Patterns. I found it same experience in Head First Java, very casual approach and like case studies....