Java Problems for Students


A set of exercises I give to students learning Java. They are ordered by complexity — each one introduces a new concept or data structure without assuming knowledge of the next.

Envelope

Print the following shape to the console:

  **********************
  *  *              *  *
  *     *       *      *
  *         *          *
  *     *       *      *
  *  *              *  *
  **********************

The goal is to reason about nested loops and coordinate-based conditions — which cells are on the border, which are on the diagonals.

Bracket validation (single pair)

Given a string, determine whether the bracket sequence is valid. Only {} pairs count.

  • Valid: {}{}, {{}}, {{}{}}
  • Invalid: }{, {}}, {}}{

A stack is the natural tool here. Push on open, pop and verify on close.

Bracket validation (all pairs)

Extend the previous solution to handle all bracket types: [], (), {}, <>. The logic is the same; the matching rule expands.

Counting Valleys

From HackerRank. A hiker walks through a landscape described as a string of U (up) and D (down) steps. Count how many valleys — sequences that go below sea level and return to it — the hiker passes through.