Weever's Road

Just another WordPress site

Author: weever (Page 1 of 4)

Blue green deployment

Concept check here

compareAndSet

1
2
3
4
5
6
7
public final boolean getAndSet(boolean newValue) {
    for (;;) {
        boolean current = get(); // line 3
        if (compareAndSet(current, newValue)) //line 4
            return current;
    }
}

if current value was changed between line 3 and line 4
The new value won’t be set and go loop until it’s done.

AtomicInteger use compare-and-set

What’s volatile and when to use it

Volatile variable tell JVM threads to read the value from main memory, thread should nor use cached value in its own stack.

Specially, volatile variable is used to signal threads.

1
2
3
4
5
private volatile boolean shutdown = false;

while(!shutdown){
 //To do
}

AtomicInteger, Volatile, synchronized

Crack interview here

Http Post and Put different

Discuss here

W3 definition

9.5 POST

The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line. POST is designed to allow a uniform method to cover the following functions:

– Annotation of existing resources;
– Posting a message to a bulletin board, newsgroup, mailing list,
or similar group of articles;
– Providing a block of data, such as the result of submitting a
form, to a data-handling process;
– Extending a database through an append operation.
The actual function performed by the POST method is determined by the server and is usually dependent on the Request-URI. The posted entity is subordinate to that URI in the same way that a file is subordinate to a directory containing it, a news article is subordinate to a newsgroup to which it is posted, or a record is subordinate to a database.

The action performed by the POST method might not result in a resource that can be identified by a URI. In this case, either 200 (OK) or 204 (No Content) is the appropriate response status, depending on whether or not the response includes an entity that describes the result.

If a resource has been created on the origin server, the response SHOULD be 201 (Created) and contain an entity which describes the status of the request and refers to the new resource, and a Location header (see section 14.30).

Responses to this method are not cacheable, unless the response includes appropriate Cache-Control or Expires header fields. However, the 303 (See Other) response can be used to direct the user agent to retrieve a cacheable resource.

POST requests MUST obey the message transmission requirements set out in section 8.2.

See section 15.1.3 for security considerations.

9.6 PUT

The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI. If a new resource is created, the origin server MUST inform the user agent via the 201 (Created) response. If an existing resource is modified, either the 200 (OK) or 204 (No Content) response codes SHOULD be sent to indicate successful completion of the request. If the resource could not be created or modified with the Request-URI, an appropriate error response SHOULD be given that reflects the nature of the problem. The recipient of the entity MUST NOT ignore any Content-* (e.g. Content-Range) headers that it does not understand or implement and MUST return a 501 (Not Implemented) response in such cases.

If the request passes through a cache and the Request-URI identifies one or more currently cached entities, those entries SHOULD be treated as stale. Responses to this method are not cacheable.

The fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI. The URI in a POST request identifies the resource that will handle the enclosed entity. That resource might be a data-accepting process, a gateway to some other protocol, or a separate entity that accepts annotations. In contrast, the URI in a PUT request identifies the entity enclosed with the request — the user agent knows what URI is intended and the server MUST NOT attempt to apply the request to some other resource. If the server desires that the request be applied to a different URI,

it MUST send a 301 (Moved Permanently) response; the user agent MAY then make its own decision regarding whether or not to redirect the request.

A single resource MAY be identified by many different URIs. For example, an article might have a URI for identifying “the current version” which is separate from the URI identifying each particular version. In this case, a PUT request on a general URI might result in several other URIs being defined by the origin server.

HTTP/1.1 does not define how a PUT method affects the state of an origin server.

PUT requests MUST obey the message transmission requirements set out in section 8.2.

Unless otherwise specified for a particular entity-header, the entity-headers in the PUT request SHOULD be applied to the resource created or modified by the PUT.

Unchecked and checked Exception

Explained here

Checked Exceptions are checked on compile time. Unchecked Exceptions are not.

How to choose unchecked or checked Exception.
If client can reasonable to be expected to recover from the exception, make it checked Exception
otherwise, make it as unchecked Exception

Pretty Print

Print concentric rectangular pattern in a 2d matrix.

Input: A = 4.
Output:

4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4

Solution:

Just mark four borders each loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    public ArrayList<ArrayList<Integer>> prettyPrint(int a) {
        ArrayList<ArrayList<Integer>> result = new ArrayList();
        if(a == 0)
            return result;
        int n = 2*a -1;
        int[][] matrix = new int[n][n];
       
        int start = 0;
        int end = n - 1;
       
        for(;a>0;a--){
            //fill top
            for(int i = start; i <=end; i++ ){
                matrix[start][i] = a; //top
                matrix[i][end] = a; //right
                matrix[end][i] = a; // bottom
                matrix[i][start] = a; //left
            }
            start++;
            end--;
        }
       
        for(int j = 0; j < n; j++){
            ArrayList<Integer> temp = new ArrayList();
            for(int m = 0; m<n; m++){
                temp.add(matrix[j][m]);
            }
            result.add(temp);
        }
       
        return result;
    }

151. Reverse Words in a String

Given an input string, reverse the string word by word.

Example:
Given s = “the sky is blue”,
return “blue is sky the”.

Trick: trim before split.

1
2
3
4
5
6
7
8
9
    public String reverseWords(String s) {
        if( s == null || s=="" || s.length() == 0)
            return s;
        String[] strs = s.trim().split("\\s+");
        List<String> list = Arrays.asList(strs);
        Collections.reverse(list);
        strs = (String[]) list.toArray();
        return String.join(" ",strs);
    }

287. Find the Duplicate Number (Hard??)

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.

Note:
You must not modify the array (assume the array is read only).
You must use only constant, O(1) extra space.
Your runtime complexity should be less than O(n2).
There is only one duplicate number in the array, but it could be repeated more than once.

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
 public int findDuplicate(int[] nums) {
        if(nums == null)
            return -1;
        boolean[] check = new boolean[nums.length];
        for(int i=0 ; i< nums.length; i++){
            if(check[nums[i]])
                return nums[i];
            else
                check[nums[i]] = true;
        }
        return -1;
    }

179. Largest Number (Meduim)

Given a list of non negative integers, arrange them such that they form the largest number.
For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.
Note: The result may be very large, so you need to return a string instead of an integer.

Solution:
1: sort the number, compare the digits of number from left to right, the largest should be first.
like: compare {89,9}, the first digit 9 is largest, sort as {9,89} so the combined 989 is largest.
2. if the digits are same like {2332,23}, we have to compare left digits with shortest number again to determine which is first, 32 > 23, so sorted as {2332,23}
3. We can find trick, for each two number, we can find the law: combine numbers in both way as strings, {89,9} = {899,989}, then compare the strings so we can sort the numbers

For list, override Collections.sort()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public String largestNumber(final List<Integer> a) {
        if(a == null)
            return "0";
        Collections.sort(a, new Comparator<Integer>(){
            @Override
            public int compare(Integer n1, Integer n2){
                String s1 = String.valueOf(n1);
                String s2 = String.valueOf(n2);
                return (s2+s1).compareTo(s1+s2);
            }
        });
        if(a.get(0) == 0)
            return "0";
       StringBuffer sb = new StringBuffer();
       for(Integer i : a){
           sb.append(i);
       }
       return sb.toString();
    }

For Arrays, write Comparator function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 public String largestNumber(int[] nums) {
        Integer[] ints = new Integer[nums.length];
        int index = 0;
        for(int i : nums){
            ints[index++] = i;
        }
        Arrays.sort(ints,new Comparator<Integer>(){
            public int compare(Integer n1, Integer n2){
                String s1=String.valueOf(n1);
                String s2=String.valueOf(n2);
                return (s2+s1).compareTo(s1+s2);
            }
        });
       
        if(ints[0] == 0)
            return "0";
        StringBuilder sb = new StringBuilder();
        for(Integer i : ints){
            sb.append(i);
        }
       
        return sb.toString();
    }

Page 1 of 4

Powered by WordPress & Theme by Anders Norén