/** * 2. Find the Sequence Sum * Find the sum of a sequence using a given formula. * Given three integers, i, j, and k, * a sequence sum to be the value of * i + (i + 1) + (i + 2) + (i + 3) + … + j + (j − 1) + (j − 2) + (j − 3) + … + k * (increment from iuntil it equals j,then decrement from juntil it equals k). * Given values i, j, and k, calculate the sequence sum as described. * Example i = 5 j = 9 k = 6 * Sum all the values from i to j and back to k: 5 + 6 + 7 + 8 + 9 + 8 + 7 + 6 = 56. * Function Description * Complete the function getSequenceSum in the editor below. * getSequenceSum has the following parameter(s): int i, int j, int k: three integers * Return * long: the value of the sequence sum * Constraints * -10^8 ≤ i, j, k ≤ 10^8 * i, k ≤ j */ publicclassQ2 { // Complete the getSequenceSum function below. staticlonggetSequenceSum(int i, int j, int k) { longsum=0;
// phase1 for (intx=0; x < j - i; x++) { sum += x + i; }
// phase2 sum += j;
// phase3 for (intx=1; x <= j - k; x++) { sum += j - x; }
/* Write a query to print the names of professors with the names of the courses they teach (or have taught) outside of their department. Each row in the results must be distinct (i.e., a professor teaching the same course over multiple semesters should only appear once), but the results can be in any order. Output should contain two columns:PROFESSOR.NAME, COURSE.NAME. */
/* Enter your query here. Please append a semicolon ";" at the end of the query */
select p.Name, c.Name from `PROFESSOR` as p innerjoin `SCHEDULE` as s on s.PROFESSOR_ID = p.id innerjoin `COURSE` as c on c.id = s.COURSE_ID where p.DEPARTMENT_ID <> c.DEPARTMENT_ID groupby1, 2 ;
/** * A restaurant ratingapplication collects ratings orvotes from its users and stores them in a database. They want to allow users to retrieve the total vote count for restaurants in a city. Implement a function,getVoteCount.Given a city name and the estimated cost for the outlet, make a GET request to the API athttps://jsonmock.hackerrank.com/api/food_outlets?city=cityNameestimated_cost=estimatedCost where cityName and estimatedCost arethe parameters passed to the function. * <p> * The response is a JSON object with the following 5 fields: * page: The current page of the results.(Number) * per_page: The maximum number of results returned per page.(Number) * total: The total number of results.(Number) * total_pages: The total number of pages with results.(Number) * data: Either an empty array or anarray with a singleobjectthat containsthe food outlets'records. * <p> * In data, each food outlet has the following schema: * id - outlet id(Number) * name: The name of the outlet (String) * city - The city in which the outlet is located (String) * estimated_cost: The estimated cost of the food in the particular outlet (Number). * user_rating: An object containing the user ratings for the outlet. The object has the following schema: average_rating: The average user rating for the outlet (Number) votes: The number of people who voted the outlet (Number) * <p> * If there are no matching records returned, the data array will be empty. In that case, the getVoteCountfunction should return -1. * <p> * An example of a food outletrecord is as follows: { "city": "Seattle", "name": "Cafe Juanita", "estimated_cost": 160, "user_rating": { "average_rating": 4.9, "votes": 16203 }, "id": 41 } * <p> * Use the votes property of each outlet to calculate the total vote count of all the matching outlets. * <p> * Function Description * Complete the getVoteCount function in the editor below. * getVoteCount has the following parameters: * string cityName:the city to query * int estimatedCost:the cost to query * <p> * Returns * int:the sum of votes for matching outlets or -1 * <p> * Constraints * No query will return more than 10 records. */ publicclassQ4 { privatestaticfinalStringAPI_URL="https://jsonmock.hackerrank.com/api/food_outlets";
publicstaticintgetVoteCount(String cityName, int estimatedCost) { return -1; }
/** * Given an interface named "OnlineAccount" that models the account of popular online video streaming platforms, perform the operations listed below. * The interface "OnlineAccount" consists of the basePrice, regularMoviePrice, and exclusiveMoviePrice. * <p> * In order to complete this challenge, you need to implement an incomplete class named "Account" which implements the "OnlineAccount" interface as well as the "ComparableAccount" interface. * <p> * Class Account has two attributes to keep track of the number of movies watched: * Integer noOfRegularMovies * Integer noOfExclusiveMovies * String ownerName * <p> * Methods to complete for class Account: * 1. Add a parameterized constructor that initializes the attributes ownerName,numberOfRegularMovies, and numberOfExclusiveMovies. * 2. Int monthlyCost() => This method returns the monthly cost for the account. [Monthly Cost = base price +noOfRegularMovies*regularMoviePrice +noOfExclusiveMovies*exclusiveMoviePrice] * 3. Override the compareTo method of the Comparable interface such that two accounts can be compared based on their monthly cost. * 4. String toString() which returns => "Owner is[ownerName] and monthly cost is [monthlyCost] USD." */ interfaceOnlineAccount { intbasePrice=120; intregularMoviePrice=45; intexclusiveMoviePrice=80; }
classAccountimplementsOnlineAccount, Comparable<Account> { int noOfRegularMovies, noOfExclusiveMovies; String ownerName;
// 1) Add a parameterized constructor that initializes the attributes noOfRegularMovies and noOfExclusiveMovies. publicAccount(String ownerName, int noOfRegularMovies, int noOfExclusiveMovies) { this.ownerName = ownerName; this.noOfExclusiveMovies = noOfExclusiveMovies; this.noOfRegularMovies = noOfRegularMovies; }
// 2. This method returns the monthly cost for the account. publicintmonthlyCost() { return basePrice + noOfRegularMovies * regularMoviePrice + noOfExclusiveMovies * exclusiveMoviePrice; }
// 3. Override the compareTo method of the Comparable interface such that two accounts can be compared based on their monthly cost. publicintcompareTo(Account account) { returnthis.monthlyCost() - account.monthlyCost(); }
// 4. Returns "Owner is [ownerName] and monthly cost is [monthlyCost] USD." public String toString() { return"Owner is " + ownerName + " and monthly cost is " + monthlyCost() + " USD."; } }
publicclassQ5 { publicstaticvoidmain(String[] args)throws Exception { /* Enter your code here. Read input from STDIN. Print output to STDOUT */