class: center, title-slide
## CSCI-UA 480: APS ## Algorithmic Problem Solving
## Introduction to the Class .author[ Instructor: Joanna Klukowska
] .license[ Copyright 2020 Joanna Klukowska. Unless noted otherwise all content is released under a
[Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/).
Background image by Stewart Weiss
] --- layout:true template: default name: section class: inverse, middle, center --- layout:true template: default name: challenge class: challenge --- layout:true template: default name: poll class: inverse, full-height, center, middle --- layout:true template: default name: breakout class: breakout --- layout:true template: default name: poll class: inverse, middle ## Poll
--- layout:true template:default name:slide class: slide .bottom-left[© Joanna Klukowska. CC-BY-SA.] --- template: section ## The Course --- template: slide ## This Course - Course website: https://cs.nyu.edu/~joannakl/aps_s21/ This page contains the syllabus and daily summaries as well as loads of links to all other resources and services you will need for this class. - Recitations (required): - Wednesdays 3:30 - 4:45pm - plan to bring your laptop (there are outlets available) - Course message board / discussion: Ed - you can self-sign up at https://us.edstem.org/ - Online judge: - Gradescope (for homework assignments and exams) - [Vjudge](https://vjudge.net/) - Grades posted on Brightspace --- template: poll ## Why are you here? --- ## What are we going to do? - Basic and advanced data structures and algorithms - review the ones you know (data structures, basic algorithms, ...) and learn a few new ones - use them to solve interesting problems - decide what data structures and algorithms should be used for which problems -- - Programming in Java and C/C++ - practice your coding skills in both - practice your debugging skills - practice testing skills -- - Bugs, edge cases, errors/issues, ... - learn how to appreciate errors/issues - use the issues you find to learn for the future - learn from each other -- - Deal with frustration ... - of your program failing tests and being convinced that the problem is NOT with your own code -- - Discuss problems, algorithms, solutions, efficiency, ... -- - Present problem solutions -- - A few invited talks --- template: challenge ## Challenge Write a program that reads two integers from standard input (i.e., the user types it on the keyboard), calculates their sum and prints the result to standard output (i.e., it appears in the terminal). - use either Java or C/C++ - write the entire program - write legibly .left-column2[ Example input: 5 9 ] .right-column2[ Example output: 14 ] --- template: challenge ## Challenge continued Exchange your code with someone sitting next to you. The code that you get should be in the progrmming language that you are familiar with (i.e., if you do not know C++ do not take code that was written in C++). -- Working with your partner's code (not the partner!), decide 1. would it compile, if it was typed exactly the way it is written 2. (assume that it compiles) would it produce the correct result if the input values were : - `35 -17` - `1000000000 -1000000000` - `1000000000 1000000000` - `-2000000000 -2000000000` - `2147483648 5` 4. is there something interesting, new-to-you, noteworthy, strange, ... in the code that you are reading? --- template: section # Online Judge (OJ) --- ## Online Judge (OJ) .big[⚖] - __black-box testing__: it does not matter what the program does and how as long as it produces correct/expected output given some input
- an online judge (black-box tester) contains many hidden tests (correctness of results and format matter!) - an online judge produces instant results (the solution either passed or failed the test) -- - readability of the code does not matter .large[😢] (well, at least note for an OJ, but writing readable code may help you in debugging the code and later on in reviewing your own solutions; it may also make it possible to show off your solutions) --- ## Typical problems Problem statement: - description of the problem - description of constraints - description of the given input format and expected output format - sample input and corresponding output -- Three parts for the problem solution: - read the input data (from standard input) - calculate results - output the results (to standard output) --- ## Workflow - Read and __understand__ the problem. -- - Think about possible solutions and pick one -- - Analyze the solutions's correctness and efficiency (if not correct or not efficient go back to the previous step) -- - Write the solution as working code -- - Test your solution using the sample input/output given in the problem -- - Test your solution using your own tests (this means you need to create inputs for which you know the correct output) -- - Submit your solution to the online judge -- - If it passes, celebrate! .large[💃] -- - If it fails, go back to one of the previous steps depending on the reason for failure --- ## Reasons for failure - __Compilation errors__ - hopefully this does not happen - __Wrong answer__ - the program completed the calculation in the given time and produced output, but the output is incorrect - __Presentation error__ - the program completed the calculation in the given time and produced output, but the output is incorrect (it differs from the correct output by whitespace) - __Runtime error__ - the program did not terminate normally (crashed with an exception or segfault) - __Time limit exceeded error__ - the program run for too long and was terminated --- template: section # Presented / Interview Problems --- ## Human Interviewer - human interviewer is the judge: your approach to the solution, clarity of explanation, elegance and efficiency are all being judged - problem may evolve as part of the discussion - there are no hidden tests, but your solution should be able to handle edge cases and unexpected inputs - readability of the code/algorithm does matters a lot
--- ## Workflow
- You are presented with a (new) problem. It may be unclear or ambiguous. -- - Make sure you understand the problem by asking followup questions. Do NOT assume anything. -- - Work through an example. This confirms your understanding of the problem to you AND to the interviewer. -- - Come up with a solution. First solution does not need to be efficient, elegant, .... DO NOT WRITE CODE. Talk through your approach, make sure it is correct (run through some examples and cover some edge cases), and analyze its performance -- - Come up with a better solution (something faster, using less memory, ...). Do similar analysis to the above. DO NOT WRITE CODE unless the interviewer asks for it. -- - If you can, come up with even better solution ... -- - Write the code or detailed pseudo-code for your solution. -- .important[ Make sure that every step of this process is a dialog. Talk through your solution. Show the interviewer how you are thinking about the problem. ] --- ## Reasons for failure -- - Making assumptions instead of asking questions. - Going directly into writing code without any discussion and analysis. - Being disorganized. - Ignoring interviewer suggestions. - Not being confident (you supposed to answer questions, not ask them). - Being too confident. - Not being prepared / not knowing the answer. - ... - --- template: section ## [Course Syllabus](https://cs.nyu.edu/~joannakl/aps_s21/) --- ## Java slow I/O .left-column2[ - Classes/objects used for reading standard input: - `Scanner` - `BufferedReader` and `InputStreamReader` - Classes/objects used for writing standard output (and more): - `PrintStream` (this is the type of `System.out`) - `BufferedWriter` and `OutputStreamWriter` In both cases, the first option is slower than the second (with `BufferedWriter` being significantly faster than the alternative). See the code examples: [InputOutput0.java](01/InputOutput0.java), [InputOutput1.java](01/InputOutput1.java), [InputOutput2.java](01/InputOutput2.java). ] -- .right-column2[ Example Executions: ``` $ time java InputOutput0 < in2.in >out real 0m7.371s user 0m3.751s sys 0m4.256s $ time java InputOutput1 < in2.in >out real 0m6.311s user 0m2.631s sys 0m4.051s $ time java InputOutput2 < in2.in >out real 0m1.426s user 0m1.780s sys 0m0.320s ``` ]