Sunday, March 14, 2021

 Given glasses of Juices of J units with capacity C units where no glass is empty, determine the maximum number of juices that can be mixed into one glass. 

 

import java.util.*; 

class Solution { 

    public int solution(int[] J, int[] C) { 

        int result = 1; 

        for (int k = 0; k < J.length; k++) { 

            int count  = 1; 

            int max = C[k] - J[k]; 

            if (max == 0) { 

                continue; 

            } 

            int[] sorted = new int[J.length - 1]; 

            int index = 0; 

            for (int i = 0; i < J.length; i++) { 

                if (i != k) { 

                    sorted[index] = J[i]; 

                    index++; 

                } 

            } 

            Arrays.sort(sorted); 

            count = 1; 

            for (int i = 0; i < sorted.length && max > 0; i++) { 

                if (max - sorted[i] >= 0) { 

                    max -= sorted[i]; 

                    count++; 

                } 

            } 

            if (count > result){ 

                result = count; 

            } 

        } 

        return result; 

    } 

} 

Correctness tests

▶simple

Simple tests.✔OK

▶no_pouring

Tests in which it is not possible to pour.✔OK

▶small_cornercases

Tests where N <= 3.✔OK

▶not_biggest_capacity

Tests in which the glass with the biggest capacity is not the one that should be picked as the base.✔OK

▶not_biggest_capacity_left

Tests in which the glass with the largest empty space is not picked as the base.✔OK

▶same_capacity

Tests in which all glasses have the same capacity.✔OK

▶full_glasses

All glasses are full.✔OK

▶almost_empty_glasses

All glasses are almost empty.✔OK

Saturday, March 13, 2021

 Writing a web chat application with Vue:

Introduction: Vue.js is a progressive framework for building user interfaces. It is one of the most popular JavaScript frameworks to build user interfaces from the ground up in an incremental manner. The core library is a view-only framework and as such can be linked into web pages directly from a content data network. For example, if we want to build a webchat application, the following article will help us get started. 

 

Description:  A sample web page to be written in Vue could start with just the template, style, and script as shown in reference 1. All of this can be on the same web page but Vue provides excellent integration with many other frameworks so it is easy to package code in single file components that end with the .vue extension. These can then be combined into the main page with the help of component declaration and use. Each view file allows specifying a template, script, and style snippet. 

 

A vue instance consists of a root Vue instance and includes a tree of nested, reusable components when the application is well-organized. The instance created with a dictionary for data and methods which tells Vue’s reactivity system how to respond when the data changes from the user interface. There is a freezing method available that can prevent the existing properties from being changed but for the most part, a vue instance responds to the user’s directives. 

 

The state can be maintained to avoid re-rendering. This requires the use of a keep-alive directive surrounding the component Together with data, methods, and state, the vue instance can be successfully tested in all three forms: unit-testing, component testing and end-to-end testing.  

 

When we build a web-chat system, we simply define the template to comprise the component items we want to show. For example, a webchat system will be created with the help of vue-quick-chat. The look and feel of the web chat can be customized using the participants, messages, onType, onMessageSubmit, chatTitle, placeholder, colors, borderStyle, hideCloseButton and icon sizes. The onType and onMessageSubmit attributes allow the user experience to be customized and connected with any backend APIs.

The data and method handlers can be integrated with any backend system that parses the content of the messages. There is only one requestor and one responder, in this example, but the webchat user interface control can is independent of the backend system that behaves as the responder. It can even allow passive forwarding of messages between a group chat simply by making calls to a backend system. If the backend system is to be avoided to make the calls, the nodeJs can forward the message from one member to the other members of the group chat.

  

Conclusion: The framework provides incrementally adoptable components that can be mixed and matched to create a view and complex controls, logic, and workflow can be broken down and packaged into granular and reusable components. 


#codingexercises
find the largest square embeddable in a bar chart
import java.util.*;

class Solution {
    public int solution(int[] A) {
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < A.length; i++) {
            for (int j = 0; j < A[i]; j++){
                int side = 1;
                for (int k = i + 1; k < A.length && k - i + 1 <= j + 1 && A[k] >= j+1; k++) {
                    side = k - i + 1;
                }
                if (side > max) {
                    max = side;
                }
            }
        }
        return max;
    }
}

Friday, March 12, 2021

Paxos algorithm:

This article explains the Paxos algorithm with reference to people as participants. A group of people wants to come to a consensus with their choice of proposals. Paxos helps them arrive at this consensus eventually even when things go wrong. It guarantees that only one of the proposals will be chosen if at all and when it is chosen, all the others will be able to learn the choice and use it. If no proposals are made then there will not be any selection. The selection here is the end result of the algorithm so that all the participants can proceed in a coordinated manner knowing that there is only one common choice by consensus.

Paxos relieves the participants from figuring out how the choice is made until they are each informed of the choice. When there are proposals, there is a guarantee that a choice will be made and that it will eventually be communicated to the participant.

In this regard, each participant works in three modes: as a proposer, an acceptor, and a learner. A proposer can make a proposal. As an acceptor, a participant makes the choice among proposals. Eventually, as a learner, a participant knows which choice was made by itself or others. Participants inform each other with messages.

There are many failures that are overcome by this algorithm. Participants may not all be working in the same manner and at the same speed. They may disappear after a choice was made. Their messages may be lost, duplicated, or delivered very late. However, the messages are tamper-proof.

Since participants may not be reliable, this algorithm differs from others that require them to be available. It helps to choose a value in one of several approaches. The first approach is when a single participant decides which proposal to make or accept. Any one of the participants can be designated as the sole acceptor. Another approach is to involve more than one participant in a set of acceptors. In such a mode, a majority is chosen.

An acceptor may choose the first proposal made in the order of their arrival. In a set of acceptors with multiple messages arriving simultaneously, there might not be a majority among the acceptors. Acceptors may have to make more than one choice.

In order to keep track of different proposals, a tuple of a proposal sequence number and proposal is chosen. If the proposals are different, they have different numbers. If a single proposal is chosen, it was accepted by the majority of the acceptors.

If a choice is made with a proposal, every higher-numbered proposal uses this choice which allows acceptors to make more than one choice but guarantee that there is only one choice by consensus. Since there has to be at least one acceptor, a single choice will be guaranteed to be made.

Thursday, March 11, 2021

 Preparation for deploying API services to the cloud (continued...)

  1. This is a continuation of the previous post. 

  2. http://ravinote.blogspot.com/2021/03/preparation-for-deploying-api-services.html


  3. Diagnostic queries: As each layer and component of the web API server create and maintain their own data structures during their execution, it helps to query these data structures at runtime to diagnose and troubleshoot erroneous behavior. While some of the queries may be straightforward if the data structures already support some form of aggregation, others may be quite involved and include several steps. In all these cases, the queries will be against a running system in as much permitted with read-only operations. 


  1. Performance counter: Frequently subsystems and components take a long time. It is not possible to exhaust diagnostic queries to discover the scope that takes the most time to execute. On the other hand, the code is perfectly clear about call sequences, so such code blocks are easy to identify in the source. Performance counters help measure the elapsed time for the execution of these code blocks. 


  1. Statistics counter: In addition to the above-mentioned diagnostic tools, we need to perform an aggregation over the execution of certain code blocks. While performance counters measure elapsed time, these counters help with aggregation such as count, max, sum, and so on. 


  2. Among the improvements to quality starting from design with the help of hooks, monitors, APIs, and other test processes, and ranging all the way through implementation to production support diagnostics, quality can be enforced with a variety of instruments. Static code analyzers, unit-tests, simulated tests, end to end automation, regression passes, continuous integration and deployment runs, fault injections, stress testing, appearances in regional languages, and many more nuances serve as instruments to enforcing quality 



Conclusion: These are only some of the preparations for the API service deployments. The public clouds offer sufficient documentation to cover many other aspects. Also, please visit the following link to my blog post for more information 

Wednesday, March 10, 2021

    Preparation for deploying API services to the cloud (continued...)

This is a continuation of the previous post.  Some more considerations follow:

  1. Ensure that the application logs information at relevant points in the code. 

  1. Although servers hosting the applications may have their own logs, it is necessary for the code that executes to log as well, because it would make that code execution transparent. 

  1. Exceptions and errors are not the only points of interest. Even success cases could be logged because they would help with auditing what has transpired so far. 

  1. Verbose logging should not be ignored since disk space is not a concern nowadays and such logging will help with new systems. 


  1. Timestamps, Host information is included in each log entry. 

  1. Timestamps are critical for time-series sequences. This is relevant not only from the perspective of identifying log entries but also provides machine local information as opposed to a global view across machines.  

  1. Host information on each entry is helpful because we may not need to know the IP address of the server corresponding to a logical name shared by others. 


  1. Key=Value pairs mentioned in the logging entry.  

  1. Very often we treat logging entries are English sentences. However, this is a machine language, and it would be better to replace something like this: 
    the share /ifs/data/foo was created. 
    To  
    Share Created:  share_name=’/ifs/data/foo’, user=’Rajamani’ 

  1. These key-value pairs come in very handy to pivot or search the data in key-value stores. 

  1. Most of the big-data vendors facilitate improved analytics when key-values are present.