sanjeevkumarray / HackerRank-SoftWare-Enginner-Role-Certification-Test

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HackerRank-SoftWare-Enginner-Role-Certification-Test

Screenshot 2024-04-12 163301

Question 1:

Maximixing the Final Element

image

image

image

image

Solutions

image

class Result {

/*
 * Complete the 'getMaxValue' function below.
 *
 * The function is expected to return an INTEGER.
 * The function accepts INTEGER_ARRAY arr as parameter.
 */

public static int getMaxValue(List<Integer> arr) {
// Write your code here
 Collections.sort(arr);
 if(arr.get(0)!=1){
     arr.set(0,1);
 }
 for(int i=1;i<arr.size();i++){
     if(arr.get(i)-arr.get(i-1)>1){
         arr.set(i,arr.get(i-1)+1);
     }
 }
 return arr.get(arr.size()-1);

}

}

Question 2:

2.Largest Number of Orders.

image

image

Solutions

image

   SELECT CUSTOMER_ID
  FROM (
SELECT CUSTOMER_ID, COUNT(ID) AS order_count
FROM ORDERS
GROUP BY CUSTOMER_ID
ORDER BY order_count DESC, CUSTOMER_ID ASC

) AS temp LIMIT 1;

Question 3:

REST API: Products in Range

1

2

3

4

5

Solutions

class Result {
/*
 * Complete the 'getProductsInRange' function below.
 *
 * URL for cut and paste
 * https://jsonmock.hackerrank.com/api/inventory?category=<category>&page=<pageNumber>
 *
 * The function is expected to return an Integer value.
 * The function accepts String category, Integer minPrice and Integer maxPrice as arguments.
 * 
 */
public static int getProductsInRange(String category, int minPrice, int maxPrice)
{
    int totalItemsInRange = 0;
    int currentPage = 1;
    int totalPages = Integer.MAX_VALUE; 
    while (currentPage <= totalPages) {
        String url = "https://jsonmock.hackerrank.com/api/inventory?category=" + category + "&page=" + currentPage;
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();
            JSONParser parser = new JSONParser();
            JSONObject jsonResponse = (JSONObject) parser.parse(response.toString());
            JSONArray data = (JSONArray) jsonResponse.get("data");
            totalPages = Integer.parseInt(jsonResponse.get("total_pages").toString());
            for (Object obj : data) {
                JSONObject item = (JSONObject) obj;
                double price = Double.parseDouble(item.get("price").toString());
                if (price >= minPrice && price <= maxPrice) {
                    totalItemsInRange++;
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (org.json.simple.parser.ParseException e) {
            e.printStackTrace();
        }
        currentPage++;
    }

    return totalItemsInRange;
}

}

About