Logo

dev-resources.site

for different kinds of informations.

Average City Temperature Calculation Interview Question EPAM

Published at
7/2/2024
Categories
java
collections
epam
coding
Author
codegreen
Categories
4 categories in total
java
open
collections
open
epam
open
coding
open
Author
9 person written this
codegreen
open
Average City Temperature Calculation Interview Question EPAM

Problem Statement: Average Temperature Calculation

You are tasked with implementing a Java method that calculates and prints the average temperature for each city from given arrays of cities and temperatures.

Constraints:

You are provided with two arrays:
String[] cities: An array where each element represents the name of a city.
int[] temperatures: An array where each element represents the temperature recorded for the corresponding city in the cities array.

The length of both arrays will be the same.
Duplicate city names may exist in the cities array, and the subsequent indices of temperatures should be considered for calculating the average temperature of each city.
Temperatures are represented as integers.

Example:

Given the following arrays:

String[] cities = {"New York", "Chicago", "New York", "Chicago", "Los Angeles"};
int[] temperatures = {75, 70, 80, 72, 85};
Enter fullscreen mode Exit fullscreen mode

Your method should output:

Average temperature in New York: 77.5
Average temperature in Chicago: 71.0
Average temperature in Los Angeles: 85.0
Enter fullscreen mode Exit fullscreen mode

Solution

// a Pair class in created to keep track of total temperatures and sum of temperature
private static class Pair{
        public int temperature;
        public int total;

        public void addNewTemperature(int temperature ){
            this.temperature = this.temperature +temperature ;
            this.total = this.total+1;
        }

        public double calculateAvgTemperature()
        {
            return (double) temperature / total;
        }
    }
Enter fullscreen mode Exit fullscreen mode
private static void printAverageTemperature(String[] cities, int[] temperatures)
    {
        Map<String,Pair> citiesMap = new HashMap<>();

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

            String currentCity = cities[i];
            // fetch existing pair or create new for current city
            Pair pair = citiesMap.get(currentCity) == null ? new Pair() : citiesMap.get(currentCity);

            // this will add new temperature to exiting, and increment total by 1
            pair.addNewTemperature(temperatures[i]);

            // add pair obj to map
            citiesMap.put(currentCity,pair);
        }


        citiesMap.forEach( (key,pair)->{
            System.out.println(key +": "+pair.calculateAvgTemperature());
        });


    }
Enter fullscreen mode Exit fullscreen mode
collections Article's
30 articles in total
Favicon
Introduction to Arrays
Favicon
Collections in Salesforce Apex
Favicon
Understanding Collections in .NET: A Guide to Simplicity and Abstraction
Favicon
Understanding the Need for Collections in Programming
Favicon
Explain Load Factors for ArrayList and HashMap in Java
Favicon
Generics in C#: Flexibility, Reusability, and Type Safety
Favicon
Explain internal working of HashMap in Java
Favicon
Common Java Libraries and Frameworks you Should Try
Favicon
What is the difference between HashMap and Hashtable?
Favicon
What are the differences between HashSet and TreeSet?
Favicon
Understanding the Difference Between pluck() and select() in Laravel 11
Favicon
Java Collections Scenario Based Interview Question
Favicon
Mastering Java Collections with Multithreading: Best Practices and Practical Examples
Favicon
Building Ansible Execution Environments with Non-Default Collections
Favicon
Average City Temperature Calculation Interview Question EPAM
Favicon
Enhancing Order in Java Collections with Sequenced Interface
Favicon
[Python] Collections.Counter
Favicon
Java Collections: Saiba quando usar Set, Map, List ou Queue.
Favicon
Ways to filter list in java
Favicon
A quick tour of collections in C#
Favicon
Collections en Java : ImplΓ©mentations et cas d'utilisation (List, Set et Map)
Favicon
#Rust πŸ¦€ – Working with hashmaps is cool. And a little C# experience πŸ˜„
Favicon
Create a Twitter RSS Feed With Upto 1000 Items (2023)
Favicon
HashMap in java
Favicon
How to Remove Duplicates from ArrayList in Java
Favicon
When to use LinkedList over ArrayList in Java ?
Favicon
Content Marketing Portfolio β€” A Step-by-Step Guide with Examples
Favicon
How to Create a PDF Portfolio & 5 Excellent PDF Portfolio Examples
Favicon
A Comprehensive Guide to Creating a Journalism Portfolio
Favicon
How to Build a Stunning Social Media Portfolio: A Step-by-Step Guide with Examples

Featured ones: