Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

isSortedBy

Related Links:
Author: Whitaker Brand

Write a method isSortedBy to be added to the LinkedIntList class that accepts an integer n as a parameter and that returns true if the list of integers is sorted in nondecreasing order when examined "by n" and false otherwise. When examining a list "by n," you pick any element of the list and consider the sublist formed by that element followed by the element that comes n later, followed by the element that comes 2n later, followed by the element that comes 3n later, and so on. For example, suppose that a variable list stores the following sequence of numbers:

[1, 3, 2, 5, 8, 6, 12, 7, 20]

This list would normally not be considered to be sorted, which means the call of list.isSortedBy(1) should return false. But when examining elements by 2, we get two sorted sublists:

[1, 2, 8, 12, 20] and [3, 5, 6, 7]

The call of list.isSortedBy(2) should return true. Notice that duplicates are allowed in the sublists.

The call of list.isSortedBy(3) returns false because one of the resulting sublists is not sorted:

[1, 5, 12] (sorted), [3, 8, 7] (NOT sorted), and [2, 6, 20] (sorted)

By definition, an empty list and a list of one element are considered to be sorted. The method should return true whenever n is greater than or equal to the length of the list, because in that case all of the resulting sublists would be of length 1. Your method should throw an IllegalArgumentException if passed an n that is less than or equal to 0.

Assume that we are adding this method to the LinkedIntList class as seen in lecture and as shown below. You may not call any other methods of the class to solve this problem and your method cannot change the contents of the list.

public class LinkedIntList {
    private ListNode front;   // null for an empty list
    ...
}
Type your solution here:


This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.