Java Exercises 101

Jesus PF
3 min readOct 10, 2020

--

I am a software developer. I do not live in the US, but I tend to look at FANG’s companies and would like to work in one of them. In order to do that I have to be on my A game in Computer Sciences.

Even though I studied my Master in C.S. from 2017–2019 I am not that used to the kind of problems that you get asked in HackerRank, topcoder or else. These platforms are highly recommended by FANG’s engineers and will help you getting to practice some Data structures.

That is the reason I started practicing on these sites and will be posting my code and results here. Feel free to post any exercise you have been doing, or sharing your solutions, or consider we should learn about something else to land these type of jobs.

Problem Statement

Retrieved from : https://arena.topcoder.com/#/u/practiceCode/17635/93998/15635/1/332941
There is a group of N >= 2 people. There are some individuals and some pairs of twins. If we write down the age of each individual and also the age of each pair of twins (but only once, not twice), the numbers we will get are all distinct integers between 1 and 200, inclusive.

You were shown a photograph. There are N-1 people in the photograph because the N-th person was taking it. For each person in the photograph you were told their age. These are given to you in the ages.

Given what you know, determine the following values:

  • Amax = the largest possible age of a pair of twins
  • Tmax = the largest possible number of pairs of twins

Return the {Amax, Tmax}. That is, return a in which element 0 is Amax and element 1 is Tmax.

Constraints

  • ages will contain between 1 and 50 elements, inclusive.
  • Each element of ages will be between 1 and 200, inclusive.
  • Each value will occur at most twice in ages.

Examples

  1. {10, 20, 30, 10, 30}
    Returns: {30, 3 }
    It is possible that there are three pairs of twins. This happens precisely when the person taking the photograph is 20 years old, and thus the twin of one of the people in the photograph.
    Regardless of the age of the missing person, the oldest pair of twins are the two 30-year-olds in the photograph
  2. {47}
    Returns: {47, 1 }
    The person taking the photograph may or may not be the twin of the person in it.
  3. {10, 50, 47, 20, 24, 43, 13}
    Returns: {50, 1 }

Understanding

So we received a primitive integer list of peoples ages
In this list, we will have at least one person since the other one is taking the picture
To calculate the Amax we only have to save the oldest person in the list. We do not care if the oldest person explicitly has a twin in the array, since it could be the one taking the picture.
To calculate Tmax we have several options. Since the list is not sorted by ages, we do not want to sort the list, nor run a big-O(N²) solution we will use a Hashset and a variable twincounter to count the twins. If al the persons in the pictures are twins, it is not possible to have an extra one. But if there is a person without a twin in the picture, there could be one.

import java.util.HashSet;public class RealTaskWithTwins{
public int[] solve(int[] ages){
int Amax=0;
int Tmax=0;

HashSet<Integer> agesSet=new HashSet<Integer>();
for( int i=0; i< ages.length;i++){
if(ages[i]>Amax) Amax=ages[i];
if(agesSet.contains(ages[i]) ) Tmax++;
agesSet.add(ages[i] );
}
if(Tmax*2 != ages.length) Tmax++;
int[] answer ={Amax,Tmax} ;
return answer ;
}
}

--

--

Jesus PF
Jesus PF

Written by Jesus PF

I am en electronics engineer, graduated from ITESM. Experienced working as functional validation and software development.

No responses yet