wiki:ProgrammingPrompts

Version 12 (modified by Timothy Lin, 9 years ago) (diff)

--

Programming Prompts

The prompts are written with Java in mind, but can be adapted to most languages. The downloadable test scripts are run with Java, require the main class name to be the name of the prompt, and use the default package.

Note: To use the starter code and test scripts at the bottom of the page, download them to the directory of your choice, then in Eclipse, right-click in the Project Explorer, click Import..., and in the dialog box, select General --> Existing Projects into Workspace. Select the directory you saved the unzipped file into for the root directory, and select all projects there.


Note: While the prompts are in beta phase, the starter code is not yet available for most of the prompts.

Checkerboard

Write a static method that takes two int parameters m and n and returns a checkerboard-patterned two-dimensional boolean array that is m-by-n in size. Any given checkerboard square on a checkerboard has no adjacent squares with the same color (boolean value).

https://upload.wikimedia.org/wikipedia/commons/thumb/7/70/Checkerboard_pattern.svg/300px-Checkerboard_pattern.svg.png

Sort3

Use only if-statements and/or nested if-statements to sort three numbers in ascending order. Do not use for-loops, or call any methods besides the ones you write. For an extra challenge, write the static method using only 3, un-nested if-statements, which is the minimum number.

Best results if you think about the code yourself, and not look it up.

Checksum

A checksum is a small integer that is transmitted at the end of a message. The value depends on the message sent. The checksum is used to determine the integrity of the message as it's sent over the wire. For more information about how checksums are used, see this video or this.

For this problem, you will implement a simple hash/checksum algorithm.

Write a static method that takes in a string and returns it's short typed checksum. Calculate the checksum using the following steps:

  1. For each character, convert the String to a char.
  2. For each char, raise it to the power of its index. (For example, the 7th char will be raised to the 6th power.)
  3. Sum the powers.
  4. Modulus divide the sum to make it fit in a short data type.

MovingAverage

A moving average is the average of the n most recent numbers. Design a data type (see the Internet for a definition) that has the following API that computes the moving average of a set of n numbers stored in the data type:

public class MovingAverage

MovingAverage(int n)

Constructor. Takes a parameter n that determines how many of the recent values to average.

void addValue(double)

Adds a value to the list. Does not directly change the average. The average should be updated immediately to reflect this addition.

double getAverage()

Returns the moving average of the most recent n values entered.

void clear()

Clears the list. getAverage() should now return 0.

RomanNumeralInterpreter

Write a static method that takes a string input argument (the Roman numerals) and returns the Arabic ("normal") equivalent. Use conventional, contemporary rules for Roman numerals (see wikipedia).

QueenCheck

Given a chess board (8-by-8) populated entirely by queens, determine if any of the queens are threatened by another. Write a static function to determine this, given a 2D boolean array where queens are represented by true.

Remember, queens threaten other chess pieces when they lie on the same row, column, or diagonal. Your static method should return true if any queens are threatened by another and false otherwise.

CodeBreaker

You will be provided a lexicon of common words in lexicon.txt which is in the same directory. Write a program that takes in a Caesar-ciphered string and decrypts it reliably (> 95% success rate). You will not be given the shift key. Do not look at the ciphertext to help with the decoding; your program needs to be able to decode it without human assistance.

IceCreamParlor

This problem involves a simulation, which is provided in the starter code for your convenience.

This problem also involves writing several classes(part a) using multithreading (part b).

Rules of the Simulation

  1. There exists an ice cream shop in the distant land of Starvedlandia which employs 6 robot employees, all identical except for one who is the manager.
  2. The simulation will run for one "day" where some human customers will come in and purchase some amount of ice cream (# of scoops) of a set of types (chocolate, vanilla, strawberry, cookies and cream, mint).
  3. All the ice cream is stored in a freezer which can only be accessed by one employee at a time.
    1. Because the robots are all paranoid that the human slaves will rebel and become sentient, they keep all the ice cream locked in the freezer with the only key in the hands of the manager.
    2. To open the freezer, an employee goes to the manager, requests the key, gets the ice cream from the freezer, and then, after relocking the freezer, returns the key to the manager.
    3. The manager can only see one person at a time.
    4. Special note: When implementing the multithreading portion of this, be aware of the deadlock possibility here, depending on your implementation.
  4. There are two cash registers available to the employees (5 out front) which they need to use to process the transaction. Each cash register can process one transaction at a time; the other employees will have to wait.

Assume an infinite amount of ice cream is stored in the freezer.

To recap and clarify, a customer comes in wanting to buy ice cream and approaches an employee (not the manager); the employee must learn how much and what type of ice cream the customer wants. To get the ice cream, they need to go to the manager, get the key, go to the freezer, get the ice cream, and return the key to the manager. Then the employee needs to find an open cash register to finish the transaction.

Part 1: Implement Classes
The following classes will be essential to be implemented. You may decide other classes are necessary. It is recommended to build from the starter code, and not modify it. public class Manager

AlternativeVote

http://sightline.wpengine.netdna-cdn.com/wp-content/uploads/2015/06/Foodtown-Legislature-square.jpeg The U.S. uses the voting system of First Past the Post (or winner-takes-all election), which can cause a candidate who has won less than the majority to win all the votes, resulting in disproportionate representation.

There is a graphical user interface (GUI) built to run alongside your code for this prompt.


Test Scripts

Test scripts are designed to help you debug your code, not for any grade/point value (what are grades?), so they are not secure.

You can download the Eclipse projects individually for each prompt, or download them all in a single zipped file (separate projects).

Attachments (5)

Download all attachments as: .zip