Java Exercises

Prima di tutto, ricordo che sul libro CS awesome ci sono 5 practice units:

Practice units: 11, 12, 13, 14, 15

Contengono parecchie domande ed esercizi, in particolare penso troverete utili le domande sulle classi. Purtroppo sono mischiate, quindi non ha molto senso che vi metta link specifici, dovrete cercarle voi.

Riguardo il nostro esame, abbiamo trattato la maggior parte del libro eccetto la ricorsione e per le classi Java richiederò solo qualche semplice esercizio di modellazione come quelli visti in classe.

Qui di seguito riporto inoltre esercizi che ho proposto agli orali più diversi altri, in particolare per matrici e hashmaps. Non è strettamente necessario farli tutti e probabilmente non ne avrete il tempo, consiglio di farne un po' per tipo in base alle aree in cui avete più bisogno di approfondire.

Gli esercizi sono divisi in sezioni:


Edabit - Pages in a Book


Edabit - Strings

Exercise - The Kingdom of Stringards

Characterland is ruled with the iron fist by the Dukes of Stringards. The towns managed by them are monodimensional, and can be represented as a string: the host dukes d, lords s, vassals v and peasants p. To separate the various social circles from improper mingling, some walls |mm|have been erected.

Stringards IV: Power struggle

Over time the dukes family has expanded and alas ruthless feuds have happened. According to the number of town people to the left of the dukes or to the right, a corresponding number of royal members to the left or to the right receives support to do their power games. A member of the dukes palace who receives support becomes uppercase. Each character 'p', 'v' or 's' contributes support (but not the walls). The royal members who are not reached by support are slaughtered by their siblings, and substituted with a Latin Cross Unicode

Example - given:

String town = "ppp|mm|vv|mm|v|s|mm|dddddddddddddddddddddddd|mm|ss|mm|vvvvv|mm|pppp";

After your code, it must print:

Members of the royal family:24

After the deadly struggle, the new town is


Construction site

Given a a list house as a list of strings nxm and a character list bricks of n+1 elements, RETURN a NEW list obtained by alternating rows of house with characters taken from bricks


Edabit - lists

Edabit - Lists - Other exercises

Geeks for Geeks - Lists - Other exercises

Exercise - sumvec


RETURN true if provided list has the first three elements ordered in ascending order, otherwise return false.

• if the list has less than 3 elements, return false

public static boolean ord3(List<Integer> lista)
ord3([5]) -> false

ord3([4,7]) -> false

ord3([4,6,9]) -> true

ord3([4,9,7]) -> false

ord3([9,5,7]) -> false

ord3([4,8,9,1,5]) -> true  # first 3 are increasing

ord3([9,4,8,10,13]) -> false # first 3 are NOT increasing

Exercise - tante_lettere


Create a function that transforms sentences ending with multiple question marks ? or exclamation marks ! into a sentence sith single sequences of exclamations


noYelling("What went wrong?????????") ➞ "What went wrong?"

noYelling("Oh my goodness!!!") ➞ "Oh my goodness!"

noYelling("I just!!! can!!! not!!! believe!!! it!!!") ➞ "I just! can! not! believe! it!"
// change repeating punctuation inside the sentence (NOTE: the requirement is different from Edabit)

noYelling("Oh my goodness!") ➞ "Oh my goodness!"
// Do not change sentences where there exists only one or zero exclamation marks/question marks.

noYelling("I just cannot believe it.") ➞ "I just cannot believe it."


Don't worry about mixed punctuation (no cases that end in something like ?!??!).

Keep sentences that do not have question/exclamation marks the same.


Given two integer numbers n and m, with m a multiple of 3, RETURN a list of n strings of length m having in the strings the digits from 0 to 2 in three vertical strips.

public static List<String> flag(int n, int m)





Check if One Array can be Nested in Another

Create a function that returns true if the first array can be nested inside the second.

arr1 can be nested inside arr2 if:

arr1's min is greater than arr2's min. arr1's max is less than arr2's max.


canNest([1, 2, 3, 4], [0, 6]) ➞ true

canNest([3, 1], [4, 0]) ➞ true

canNest([9, 9, 8], [8, 9]) ➞ false

canNest([1, 2, 3, 4], [2, 3]) ➞ false

Note: the strict inequality (see example #3).

Exercise - breakdance

✪✪ As a skilled breakdancer, you're given music as a list of sounds. You will have to perform a couple of dances:

Example - given:

List<String> music = List.of('unz','pa','pa','tud','unz','pa','pa','tud','unz','boom','boom','tud')




Exercise - Wild West

✪✪ The two outlaws Carson and Butch agreed to bury a treasure in the jolly town of Tombstone, ma now each of them wants to take back the treasure without sharing anything with the partner.

Write some code which prints the run and terminates as soon as one them arrives to the last city, telling who got the treasure.

Example - 1 given:

#             0         1          2           3            4               5
List<String> road = List.of("Santa Fe","Denver","Dodge City", "Silverton", "Agua Caliente", "Tombstone");
int carson = 3, butch = 0;

it must print:

Carson starts from Silverton
Butch starts from Santa Fe
Carson reaches Agua Caliente
Butch reaches Dodge City
Carson reaches Tombstone
Butch reaches Agua Caliente

Carson takes the treasure in  Tombstone !

Example 2 - given:

#            0         1          2           3              4               5
List<String> road = List.of("Santa Fe","Denver","Dodge City", "Silverton", "Agua Caliente", "Tombstone");
int carson = 3, butch = 2;

it must print:

Carson starts from Silverton
Butch starts from Dodge City
Carson reaches Agua Caliente
Butch reaches Agua Caliente
Carson reaches Tombstone
Butch reaches Tombstone

Final duel in  Tombstone !


Edabit - Matrices - Other exercises

GeeksForGeeks - matrices - other exercises

Leetcode - matrices - other exercises

Sets and Maps

Coding Bat - maps - exercises

Geeks for geeks - maps - exercises

If you don't know how to do them, notice there is often on Editorial tab with an article explaining them.

Edabit - maps - exercises

Leetcode - maps - Missing Number

Edabit - Odd One Out

ATTENZIONE: ci sono due modi per farlo: quello semplice è contare le frequenze con le hashmap ma richiede più memoria. Un modo alternativo più efficiente, ma più difficile, è usare solo con quattro variabili intere.

              5       3      3      3        
oddOneOut(["silly", "mom", "let", "the"]) ➞ true

               6         5       6
oddOneOut(["swanky", "rhino", "moment"]) ➞ true

             3      4        5
oddOneOut(["the", "them", "theme"]) ➞ false

              4      2    2      4
oddOneOut(["very", "to", "an", "some"]) ➞ false

            1     2
oddOneOut(["a", "bc"]) ➞ false

            3       3      3       3       5 
oddOneOut(["dir", "dac", "inc",  "tra", "mondo"]) ➞ false



CSAwesome practice units

See class questions and exercises in CS awesomepractice units: 11, 12, 13, 14, 15 .

Exercises types are mixed, you will have to browse a bit to find them.

Edabit - classes - CoffeeShop