wiki:ProgrammingPrompts

Programming Prompts

The prompts are written with Java in mind, but can be adapted to most languages. Download the starter code and test scripts at the bottom of the page to start.

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.

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).
The top, left block is black and true.

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 long-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+1. (For example, the 7th char with index 6 will be raised to the 7th power.)
  3. Sum the powers.
  4. Allow overflow.

Note that you must use a numeric primitive type, and not a reference type like BigNumber, and the checksum must consistently fit in 64 bits (size of a long).

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, which is allowed to be 0. 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.

There are many ways to implement this class; consider the order-of-growth for various implementations (running time efficiency & memory usage).

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).

Limit the Roman numeral to a max of 5,000 and throw an java.lang.IllegalArgumentException if the limit is exceeded or the Roman number is invalid.

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.

Special Case

  • You should throw an java.lang.IllegalArgumentException if the given boolean array is not the standard size.

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 or answers to help with the decoding; your program needs to be able to decode it without human assistance.

Do not modify any characters beside the upper and lowercase alphabet.

IceCreamParlor

http://www.hallsley.com/files/2013/07/7-scoop-ice-cream-cone.jpg

This problem involves writing several classes using multithreading.

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


public class Freezer


public class Employee


public class Customer


public class Cash Register

Part 2: Implement the Simulation

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.

A reasonable alternative to this system, which would discourage the spoiler effect, is the alternative vote, also known as instant-runoff voting. Read more about the alternative vote here and/or watch this video explaining the process (more fun).

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

Implement a instantaneous (live) ballot counter for the alternative voting system. You will write a class that will be given a list of Vote objects (defined below and declared in the code) and must process them and determine any winners along the way.

public enum Party

ALEXANDRIAN_PARTY, NATHANS, ANDIAN PARTY

public enum Candidate

ALEX, NATHAN, ANDREW, JACOB, ARDEN

public void setParty(Party)

Candidates' parties are variable and determined at runtime.

public class Vote

public Party getParty()

public Candidate getCandidate()

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. Occasionally they contain solutions; try not to be tempted.

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

Last modified 7 years ago Last modified on Dec 11, 2015, 10:44:41 PM

Attachments (5)

Download all attachments as: .zip