COS 126 Exam Review - Princeton University …algorithms has rarely been directly accessible to...

16
COS 126 Exam Review • Exams overview •Example programming exam • Example written exam questions (part 1)

Transcript of COS 126 Exam Review - Princeton University …algorithms has rarely been directly accessible to...

Page 1: COS 126 Exam Review - Princeton University …algorithms has rarely been directly accessible to practitioners, researchers, or students. An Introduction to the Analysis of Algorithms,

COS 126 Exam Review

•Exams overview •Example programming exam •Example written exam questions (part 1)

Page 2: COS 126 Exam Review - Princeton University …algorithms has rarely been directly accessible to practitioners, researchers, or students. An Introduction to the Analysis of Algorithms,

Exams overview (revisited)

2

We have exams in the fall

• Two written exams.

• Two programming exams.

• Prep sessions in class meetings

• No exam "midterm week"

• No final exam

Programming exams.

• October 10 and December 5.

• Mini in-class assignments.

• “Can you write a short program?”

• You will need to practice.

Written exams.

• October 17 and December 12.

• Written by RS.

• “Did you watch the lectures and do the reading?”

• One question per lecture (roughly).

Note: RS has been known to re-use questions

Page 3: COS 126 Exam Review - Princeton University …algorithms has rarely been directly accessible to practitioners, researchers, or students. An Introduction to the Analysis of Algorithms,

Exams tab on the booksite

3

See Exams tab for full details and old exams.

• Read carefully before each exam.

• Policies are the contract between us and you.

Policies (programming exam).

• Open course materials.

• No other web access.

• No outside communication.

Watch this space for details

Policies (written exam).

• Closed book/notes/computer.

• 1 page (one side) cheatsheet.

Page 4: COS 126 Exam Review - Princeton University …algorithms has rarely been directly accessible to practitioners, researchers, or students. An Introduction to the Analysis of Algorithms,

Things to remember about inclass exams

4

We know that you don't have much time.

• Exams are 50 minutes.

• "One page" programming exams.

• Five-minute questions on written exams.

We have to grade the exams.

• 400+ exams.

• No open-ended questions.

• Fully prepared rubrics.

Exams are only part of the story.

Old exams are not completely reliable.

• Course offerings differ slightly.

• We have made mistakes in the past.

Page 5: COS 126 Exam Review - Princeton University …algorithms has rarely been directly accessible to practitioners, researchers, or students. An Introduction to the Analysis of Algorithms,

Programming Exam Logistics

5

Writing a short program in 50 minutes can be a challenge for anyone.

• You will use your own computer.

• You will download and edit a template.

• You will submit your solution in the same way as you do for assignments.

You don't all fit in this room.

• Pay attention and know where to go.

• Arrive early.

• Make sure your computer is charged.

Advice: Practice, practice, practice.

• Write some short programs on your own.

• Attend the practice programming exam.

• Try a past programming exam (untimed).

• Try another one (timed).

Page 6: COS 126 Exam Review - Princeton University …algorithms has rarely been directly accessible to practitioners, researchers, or students. An Introduction to the Analysis of Algorithms,

Programming Exam 1 Part 1

6

Example (Fall 2015).

Part 1. Write programs that find the number of distinct values among the integers on standard input, assuming that the input is nonempty and in sorted order.

Your task. Add code to the file Count1.java to print the number of integers on standard input and the number of distinct values among those integers.

Q. Can you write a simple program on your own ?

public class Count1{ public static void main(String[] args) { int count = 1; int distinct = 1; // YOUR CODE HERE } }

Exams Info gave instructions to load this before the exam

Details. Write a single loop that uses StdIn.readInt() to read each integer one at a time, but do not save them in an array. To compute the number of distinct values, add code to the loop to update distinct if the new value just read differs from the value read just before it.

% more testCount1tiny.txt1 1 1 1 2 2 2 2 4 4 4 4 5 5 6 6 9 9

% java Count1 < testCount1tiny.txt6 distinct values among 18 integers

you also get a test file and desired output

Page 7: COS 126 Exam Review - Princeton University …algorithms has rarely been directly accessible to practitioners, researchers, or students. An Introduction to the Analysis of Algorithms,

Programming Exam Strategy

7

Your task. Add code to print the number of integers on standard input and the number of distinct values among those integers.

Write and submit the easiest code before tackling the hard part.

public class Count1{ public static void main(String[] args) { int count = 1; int distinct = 1;

} }

and the number of distinct values among those integers. // number of integers

// number of different ones

// count the integers

// count the different ones

if (newVal != val){ distinct++; val = newVal; }

int val = StdIn.readInt(); while (!StdIn.isEmpty()) { int newVal = StdIn.readInt(); count++;

StdOut.println(distinct + " distinct values among " + count + " integers");

}

% more testCount1tiny.txt1 1 1 1 2 2 2 2 4 4 4 4 5 5 6 6 9 9

% java Count1 < testCount1tiny.txt1 distinct values among 18 integers

% java Count1 < testCount1tiny.txt6 distinct values among 18 integers

Page 8: COS 126 Exam Review - Princeton University …algorithms has rarely been directly accessible to practitioners, researchers, or students. An Introduction to the Analysis of Algorithms,

Programming Exam 1 Part 2

8

Example (Fall 2015).

Part 2. Assume that the integers on standard input are nonnegative and less than a value M given as the first command-line argument, but not necessarily in order.

Q. Can you quickly apply something you have recently learned ?

public class Count2{ public static void main(String[] args) { int M = Integer.parseInt(args[0]); boolean[] b = new boolean[M]; // YOUR CODE HERE } }

Exams Info gave instructions to load this before the exam

Details. To compute the needed values, use two loops. First, write a loop that reads the integers one at a time from standard input, counts them, and uses the boolean array b[] to record which values have been seen: when you read a value val, set b[val] to true. Second, write a loop that counts the number of true values in b[] (the number of distinct values in the input). 4 1 4 1 5 9 2 6 2 4 1 4 1 5 9 2 6 2

the description includes an example

i 0 1 2 3 4 5 6 7 8 9

b[i] F T T F T T T F F TQ. Do you understand "coupon collector" ?

Page 9: COS 126 Exam Review - Princeton University …algorithms has rarely been directly accessible to practitioners, researchers, or students. An Introduction to the Analysis of Algorithms,

Programming Exam 1 Part 2 Fall 2015 Solution

9

public class Count2{ public static void main(String[] args) { int M = Integer.parseInt(args[0]); // no number higher than M - 1 boolean[] b = new boolean[M]; // true means index was input // loop to read input, count integers and fill boolean array int count = 0; while (!StdIn.isEmpty()) { int val = StdIn.readInt(); b[val] = true; count++; } // count distinct numbers int distinct = 0; for (int i = 0; i < M; i++) if (b[i]) distinct++; // output StdOut.println(distinct + " distinct values among " + count + " integers"); } }

Page 10: COS 126 Exam Review - Princeton University …algorithms has rarely been directly accessible to practitioners, researchers, or students. An Introduction to the Analysis of Algorithms,

Written Exam Logistics

10

The first exam is on Thursday Oct. 17.

• Prep session (first half) next.

• Prep session (second half) Tuesday Oct. 15.

You don't all fit in this room.

• Pay attention and know where to go.

• Arrive early.

• No calculator/phone/computer/headphones

Advice.

• Review lectures/reading. • Try an old exam (untimed). • Try another one (timed). • Review a few more.

Page 11: COS 126 Exam Review - Princeton University …algorithms has rarely been directly accessible to practitioners, researchers, or students. An Introduction to the Analysis of Algorithms,

Example question: Basics

Q. Do you understand types and Java's type conversion and precedence rules ?

11

Ex. ( Fall 2014 Question 1) Give the type and value of each of the following Java expressions, supposing that it is used as the argument of a println() call.

(3 < 2) && (1 > 0) boolean FALSE

"800" + 99 String 80099

800 + 99 + "A" String 899A

3 + (int) Math.random() int 3

(double)(3 / 2) double 1.0

3 / 2.0 + 2 / 5 double 1.5

(8 <= 2) || (2e88 <= 88e2) boolean FALSE

Integer.parseInt("8.5*2") ILLEGAL

8 / 2 * (2 + 2) int 16

% jshell | Welcome to JShell -- Version 11.0.2 | For an introduction type: /help intro

jshell> (3 < 2) && (1 > 0) $1 ==> false

jshell> "800" + 99 $2 ==> "80099"

jshell> 800 + 99 + "A" $3 ==> "899A"

jshell> 3 + (int) Math.random() $4 ==> 3

jshell> (double)(3 / 2) $5 ==> 1.0

jshell> 3 / 2.0 + 2 / 5

Page 12: COS 126 Exam Review - Princeton University …algorithms has rarely been directly accessible to practitioners, researchers, or students. An Introduction to the Analysis of Algorithms,

Precedence matters

12

Many respondents were certain the answer was 16. Others … insisted the right answer was 1. That’s when the trash talking began. “Some of y’all failed math and it shows,” said one. Another posted a photo showing that even two different electronic calculators disagreed. The normally reassuring world of math, where right and wrong exist, and logic must prevail, started to seem troublingly, perhaps tantalizingly, fluid.

R O B E R T S E D G E W I C K K E V I N W A Y N E

[Sedgewick and Flajolet] are not only worldwide leaders of the field, they also are masters of exposition. I am sure that every serious computer scientist

will find this book rewarding in many ways. —From the Foreword by Donald E. Knuth

Despite growing interest, basic information on methods and models for mathematically analyzing algorithms has rarely been directly accessible to practitioners, researchers, or students. An Introduction to the Analysis of Algorithms, Second Edition, organizes and presents that knowledge, fully introducing primary techniques and results in the field.

Robert Sedgewick and the late Philippe Flajolet have drawn from both classical mathematics and computer science, integrating discrete mathematics, elementary real analysis, combinatorics, algorithms, and data structures. They emphasize the mathematics needed to support scientific studies that can serve as the basis for predicting algorithm performance and for comparing different algorithms on the basis of performance.

Techniques covered in the first half of the book include recurrences, generating functions, asymptotics, and analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples are included throughout to illustrate applications to the analysis of algorithms that are playing a critical role in the evolution of our modern computational infrastructure.

Improvements and additions in this new edition include

n Upgraded figures and code n An all-new chapter introducing analytic combinatorics n Simplified derivations via analytic combinatorics throughout

The book’s thorough, self-contained coverage will help readers appreciate the field’s challenges, prepare them for advanced results—covered in their monograph Analytic Combinatorics and in Donald Knuth’s Art of Computer Programming books—and provide the background they need to keep abreast of new research.

ROBERT SEDGEWICK is the William O. Baker Professor of Computer Science at Princeton University, where was founding chair of the computer science department and has been a member of the faculty since 1985. He is a Director of Adobe Systems and has served on the research staffs at Xerox PARC, IDA, and INRIA. He is the coauthor of the landmark introductory book, Algorithms, Fourth Edition. Professor Sedgewick earned his Ph.D from Stanford University under Donald E. Knuth.

The late PHILIPPE FLAJOLET was a Senior Research Director at INRIA, Rocquencourt, where he created and led the ALGO research group. He is celebrated for having opened new lines of research in the analysis of algorithms; having systematized and developed powerful new methods in the field of analytic combinatorics; having solved numerous difficult, open problems; and having lectured on the analysis of algorithms all over the world. Dr. Flajolet was a member of the French Academy of Sciences.

informit.com/aw | aofa.cs.princeton.edu

Cover design by Chuti Prasertsith Cover illustration by

Text printed on recycled paper

$79.99 U.S. | $83.99 CANADA

Com

puter ScienceA

N IN

TE

RD

ISC

IPL

INA

RY

AP

PR

OA

CH

SEDGEWICK

WAYNE

Programming | Algorithms

ComputerScience

An Interdisciplinary Approach

“ When arithmetic operators have the same precedence, the order is determined by left associativity, so that a - b - c and (a - b) - c represent the same sequence of operations. ”

− Sedgewick and Wayne, p. 17

not "fluid" to a computer scientist!

1

16

Page 13: COS 126 Exam Review - Princeton University …algorithms has rarely been directly accessible to practitioners, researchers, or students. An Introduction to the Analysis of Algorithms,

Example question: Arrays

Q. Do you understand basic properties and rules about Java arrays ?

13

Ex. ( Fall 2016 Question 2) Which of the following statements are true for Java arrays? Mark each statement as either TRUE or FALSE.

An array can't simultaneously store both an element of type double and an element of type boolean.

TRUE

Once you create an array, you cannot change its type. TRUE

You must access the elements in an array in sequential order, e.g., you cannot access a[5] until you have accessed a[0], a[1], through a[4].

FALSE

If a[] is a boolean array of length 126, then the expression a[i] will evaluate arbitrarily to either true or false if the index i is equal to 126.

FALSE

If a[] and b[] are two arrays of length 2, then a == b is true if and only if both a[0] == b[0] and a[1] == b[1] are true.

FALSE

Page 14: COS 126 Exam Review - Princeton University …algorithms has rarely been directly accessible to practitioners, researchers, or students. An Introduction to the Analysis of Algorithms,

Example question: Loops and conditionals

Q. Can you figure out the effect of a simple Java program that uses while and if statements ?

14

Ex. ( Fall 2014 Question 2) Fill in the trace for just after each iteration of the outer for loop in this program:

int[] a = new int[N];a[0] = 1; for (int i = 1; i < N; i++) { int sum = 0; for (int j = 0; j < i; j++) sum = sum + a[j]; a[i] = 1 + (2 * sum) / i; }

i sum a[i]

0 1

1 1 3

2 4 5

3 9 7

4 16 9

5 25 11

6 36 13

Write one line of code that could replace the body of the outer loop. a[i] = 2*i + 1;

Page 15: COS 126 Exam Review - Princeton University …algorithms has rarely been directly accessible to practitioners, researchers, or students. An Introduction to the Analysis of Algorithms,

Example question: Input and output

Q. Do you understand basic ways of communicating with your programs ?

15

Ex. ( S2011 Q4) Give the results of invoking this program with the given commands.

public class Q4{ public static void main(String[] args) { int curr = StdIn.readInt(); StdOut.print(curr + " "); int prev = curr; while (!StdIn.isEmpty()) { curr = StdIn.readInt(); StdOut.print((prev + curr) / 2 + " "); prev = curr; } StdOut.println(); } }

2

% more input.txt

2 4 6 8 10 12 8 2

% java Q4 < input.txt

2 2 4 6 8 10 10 7

% java Q4 < input.txt | java Q4

Note: It prints the first number, then the average of each number and its predecessor.

3 5 7 9 11 10 5

Page 16: COS 126 Exam Review - Princeton University …algorithms has rarely been directly accessible to practitioners, researchers, or students. An Introduction to the Analysis of Algorithms,

Mark your calendar

16

Tuesday October 8: Practice programming exam

Thursday October 10: PROGRAMMING EXAM

Tuesday October 15: Written exam prep (part 2)

Thursday October 17: WRITTEN EXAM