Wednesday, March 10, 2021

Java Interview Questions

 1. Fail Fast and Fail-Safe

      Ans- Fail Fast- iterator over-collection and removing the element from the collection.

       fail-safe is a concurrent package because use a copy of the collection


2. Why use generics

Ans- In Generics Type parameters provide a way for you to re-use the same code with different inputs


3. Comparable and Comparator

Comparable

Comparator

1) Comparable provides a single sorting sequence. In other words, we can sort the collection on the basis of a single element such as id, name, and price.

The Comparator provides multiple sorting sequences. In other words, we can sort the collection on the basis of multiple elements such as id, name, and price etc.

2) Comparable affects the original class, i.e., the actual class is modified.

Comparator doesn't affect the original class, i.e., the actual class is not modified.

3) Comparable provides compareTo() method to sort elements.

Comparator provides compare() method to sort elements.

4) Comparable is present in java.lang package.

A Comparator is present in the java.util package.

5) We can sort the list elements of Comparable type by Collections.sort(List) method.

We can sort the list elements of Comparator type by Collections.sort(List, Comparator) method.

*image source- https://www.javatpoint.com/difference-between-comparable-and-comparator


Monday, April 13, 2020

Last Stone Weight



We have a collection of stones, each stone has a positive integer weight.

Each turn, we choose the two heaviest stones and smash them together. Suppose the stones have weights x and y with x <= y. The result of this smash is:
If x == y, both stones are totally destroyed;
If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x.

At the end, there is at most 1 stone left. Return the weight of this stone (or 0 if there are no stones left.)


class LastStoneWeight
{
     public int lastStoneWeight(int[] stones)
     {
        PriorityQueue<Integer>  pq= new PriorityQueue<Integer>(Collections.reverseOrder());
     
       for(int item:stones)
      {
           pq.offer(item);
      }

      while(pq.size()>1)
      {
           pq.offer(pq.poll()-pq.poll());
       }
           return pq.peek();
      }
}

Friday, April 10, 2020

Single Number






Given a non-empty array of integers, every element appears twice except for one. Find that single one.

Example 1: Input: [2,2,1] 
Output: 1
Example 2: Input: [4,1,2,1,2] 
Output: 4

class SingleNumber
{
   public int singleNumber(int[] nums) 
  {
     int x=nums[0];
     for(int i=1;i<nums.length;i++)
     {
         x=x^nums[i];
      }

     return x;
    }

}



Friday, October 21, 2016

Execute failed: java.io.IOException: Cannot run program "wsgen": CreateProcess error=2,

I was running the Ant build for a small webservice standalone program I was getting below error -

Buildfile: C:\workspace\WebserviceExample\build.xml
wsgen:

BUILD FAILED
C:\workspace\WebserviceExample\build.xml:5: Execute failed: java.io.IOException: Cannot run program "wsgen": CreateProcess error=2, The system cannot find the file specified

Total time: 413 milliseconds

This was my build.xml file-

<project default="wsgen">
 
 <target name="wsgen" >
   
  <exec executable="wsgen">
      
   <arg line="-cp ./bin -keep -s ./src -d ./bin com.myfirst.wsServer.SayHello"/>
      
  </exec>
      
 </target>
 
</project>


After providing the complete path for wsgen in exec tag it worked like a charm-

<project default="wsgen">
 
 <target name="wsgen" >
   
  <exec executable="C:\Program Files\Java\jdk1.7.0_79\bin\wsgen">
      
   <arg line="-cp ./bin -keep -s ./src -d ./bin com.myfirst.wsServer.SayHello"/>
      
  </exec>
      
 </target>
 
</project>

Hope it will help.





Thursday, February 11, 2016

Pre-installed MYSQL Server and WAMP MYSQL Server clash due to same port

I was having issue with installing WAMP server. Every things looks fine to me but WAMP server didn't turned to Green from yellow. Tried all the way available on google but didn't work.

Initially I thought it may be due to Apache server port, look for service using 80 port but there was none. Then I look more closely and it seems my Apache server is running fine it is my pre-installed MySQL server which was causing the conflict between WAMP MySql server

Stopped Mysql server-


Started MySQL server is still having Start/Resume Service as green-


While Started Apache server had Start/Resume Services as grey-


I opened the my.ini file and look for the port number and changed from default 3306 to 3305 or it can be any free avialable port.
[mysqld]
port=3306
changed to 
port=3305

It worked like charm for me. Hope someone also might get help from this blog.


MySQL said: Documentation #1045 - Access denied for user 'root'@'localhost' (using password: NO)


While installing the WAMP server I was having the issue of MYSQL connection as I had provided my own passward in MYSQL-

1045 - Access denied for user 'root'@'localhost' (using password: NO)  error when typing http://localhost/phpmyadmin in browser.
Solution:
Go to file C:\wamp\apps\phpmyadmin4.1.14\config.inc.php
Go to the line $cfg['Servers'][$i]['password']='' and change this to
$cfg['Servers'][$i]['password']='NO'
Try opening phpMyAdmin again, and hopefully  message will read as "Access denied for user 'root'@'localhost' (using password: YES)
Now change the password in the above line to 'yourpassword' (whatever you had set it to before)

And here we go-