Posts

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

Scala Learning: Finding Square Root

I am renewing my learning in Scala and I have started the course Functional Programming Principles in Scala  in Coursera. Here is some code which is taken from that course object SqrtCalculator extends App{ def abs(x: Double): Double = if(x < 0) - x else x def sqrt (x: Double) : Double = { def sqrtIterator(guess: Double): Double = if (isGoodEnough(guess)) guess else sqrtIterator(improve(guess)) def isGoodEnough(guess: Double): Boolean = abs(guess * guess - x) / x < 0.0001 def improve(guess: Double): Double = (guess + x / guess) / 2 sqrtIterator(1.0) } println(sqrt(2)) println(sqrt(3)) println(sqrt(4)) println(sqrt(1e60)) } Output: 1.4142156862745097 1.7320508100147274 2.0000000929222947 1.0000000031080746E30 Process finished with exit code 0

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

Deleting a hidden user in MAC

If you have a normal user and want to delete, you can go to System Preference => User Groups => Unlock Pan => Select User to Remove => Click "-" (minus) Button But if user is hidden, the easiest way is to do it with command line. Here is the process Open "Termial" Run command "sudo dscl . delete /Users/<hidden user's short name>" You are done

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.  Java Concurrency in Practi