LearnFrenzy
Sign in Take a free test

Arrays in Java · Exercise 4 of 4

Arrays in Java Java Array Programs

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Classic array programs asked in Java interviews and placement tests. Write each one yourself first, then open the solution – every solution was compiled and run on Java 21, and the output shown is the real output.

Question 1 Easy

Remove duplicates from a sorted int array in place with two pointers, return the number of unique elements, and print the unique part.

Question page & discussion
Question 2 Medium

Given an array and a target, print the indices of two numbers that add up to the target, first with a brute-force double loop and then with a sorted two-pointer approach on a copy.

Question page & discussion
Question 3 Easy

Merge two sorted int arrays into a new sorted array in O(n + m) with three indexes.

Question page & discussion
Question 4 Easy

Move all zeros in an array to the end while keeping the order of the non-zero elements, in place.

Question page & discussion
Question 5 Medium

Multiply two matrices using 2D arrays (checking that the dimensions are compatible) and print the result row by row.

Question page & discussion
Question 6 Easy

An array contains the numbers 1 to N with exactly one missing. Find it with the sum formula and again with XOR (which cannot overflow).

Question page & discussion
Question 7 Medium

Print all "leaders" of an array – elements greater than every element to their right – in their original order, in O(n) by scanning from the right.

Question page & discussion
Question 8 Hard

Print the elements of an R × C matrix in clockwise spiral order using four boundaries (top, bottom, left, right).

Question page & discussion
Question 9 Easy

Read numbers in the range 0–100, count how often each appears using an int array, and print every value that appears more than once plus the most frequent value.

Question page & discussion
Question 10 Hard

Find the contiguous subarray with the largest sum, printing the sum and the subarray itself, using Kadane's algorithm extended to track indexes.

Question page & discussion
All exercises doneBack to Arrays in JavaRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links