Create a Pull Request. When your Instant Answer is ready for an initial review, create a pull request.Be sure to paste the link to this Instant Answer page in the PR description.
This blog post is a coding and system design interview cookbook/cheatsheet compiled by me with code pieces written by me as well. There is no particular structure nor do I pretend this to be a comprehensive guide. Also there are no explanations as the purpose is just to list important things. Moreover, this blog post is work-in-progress as I’m planning to add more things to it.
As always, opinions are mine.
Just replace the queue in BFS with stack and you are done:
Generally a recursive algorithm attempting candidates and either accepting them or rejecting (“backtracking”) based on some condition. Below is an example of permutations generation using backtracking:
Technique to determine result using previous results. You should be able to reason about your solution recursively and be able to deduce dp[i] using dp[i-1]. Generally you would have something like this:
When you need to figure out if something belongs to the same set you can use the following:
Below are recursive and iterative versions of inorder traversal
Simplest way to remember Djikstra is to imagine it as BFS over PriorityQueue instead of normal Queue, where values in queue are best distances to nodes pending processing.
Hopefully no one asks you this one. It would be too much code here, but the idea is not that difficult: build LPS (example “AAABAAA” -> [0,1,2,0,1,2,3]); use LPS to skip when comparing as if you were doing it in brute-force.
Let’s imagine we have window of length k and string s, then the hash for the first window will be:
H(k) = s[0]*P^(k-1) + s[1]*P^(k-2) + … + s[k]*P^0
Moving the window by one, would be O(1) operation:
H(k+1) = (H(k) – s[0]*P^(k-1)) * P + s[k+1]*P^0
Here is some sample code:
JUnit User guide: https://junit.org/junit5/docs/current/user-guide/
Many interview problems can be solved by using right data structure.
Well, internet is just full of all kinds of resources and blog posts on how to prepare for the technical interview plus there are numerous books and websites available. LeetCode is probably the most known of practicing websites. “Cracking The Coding Interview” is probably the most known book. I can also recommend “Guide to Competitive Programming”. As of system design, preparation resources are not so readily available. One good resource is “Grokking System Design Interview”, but it would be more valuable to practice designing large distributed systems. I also found the book “Designing Data-Intensive Applications” to be extremely helpful.
What are other “must know” algorithms and important system design topics to be listed in this post?
PROLOG (PROgramming in LOGic) - Cheat SheetUnder construction ...
Prolog originated in France [University of Marseilles(Prolog : Manuel de Reference et d'Utilisation by P. Roussel,Groupe d'Intelligence Artificielle, Marseille-Luminy, 1975]as a language for deductively analyzing logical arguments.
Its implementation makes it a relatively simple tool for parsingproblems which can be phrased in terms of relations which havea natural representation as tree structures.Two easily appreciated examples are - genealogy andEnglish grammar.
It also has a powerful unification mechanism which allows quickprototyping of ``template' or ``pattern matching' systems.
Here are three distinct ways of describing the form and actions of aProlog program. (Other interpretations are also useful.)
Like normal imperative programming languages, a Prolog programconsists of procedures, which are collections of statements.A program is executed by running a sequence of statements.Each of these statements executes by 'calling' a procedure, which callsother procedures, etc.
A Prolog program consists of a collection of theorems (or rules)and axioms (its assertions). Running a program consists of askinga query (a set of 'goals') to see if it is provable fromthe axioms by using the existing theorems.
Examples of some assertions:
To determine what can be 'proved' from the given assertions andtheorems, a user issues a query:
The above are examples of ground queries, meaning there areno variables in the goal. Variables (anything which begins withan upper case letter) can be used instead.
Because of its 'pattern matching/unification' mechanismfor binding actual arguments to formal arguments,Prolog provides a quick way to retrieve informationstored in the fact base.
Consider the facts of before:
These can be considered simply as either data structures,or access procedures to extract information from thesestructures.
Unit clauses can be thought of as data structures andprocedures can be thought of as rules for extractingdata which is not hard-wired as a fact.
Term | Definition |
---|---|
statement | one of fact, rule, or query |
fact (cf.unit clause) | record (cf.data model) of assumed true information |
rule (cf.predicate) | deductive formulation whereby new facts can be inferred from existing facts |
query | a statement asking about the existence of a fact(s) |
logic program | collection of rules |
meaning of a logic program | collection of facts that can be deduced from any set of initial facts |
Entity | Examples |
---|---|
atom | homer 1.5 'plan-9' 'My name is Nobody' |
operators (are also atoms) | + - :- < > |
symbol - atoms are a special case of symbol | 32 |
string (is not a defined data type in PROLOG, but really a list of ASCII values) | 'Joe Friday' 'Son of Sam' |
variable - identifier beginning with uppercase letter or underscore | X Xs Joes_IQ _total |
Under construction ...
Entity | Explanation | Examples |
---|---|---|
list | series of terms | [Name1, Name2 | Names] |
Task to Accomplish | Commands | Comments |
---|---|---|
starting PROLOG - at Unix $> prompt | $> pl | On ICC machines, your path variable must contain /usr/local/bin for this to work. Get help changing your .cshrc file if necessary. |
stopping PROLOG - at | ?- prompt | | ?- halt. | |
consultation mode from terminal | consult(user) | User controls logical and data operation. |
consultation mode from file | consult(<filename>) | User controls logical operation, but data are in file. |
asserting facts | assert(<term>) | User controls data operation. |
retracting facts | retract(<rule-head>) | User controls data operation. |
Notation - parameters prefixed by:
| |
predicate | examples |
---|---|
consult(+Files) | |
reconsult(+Files) | |
Control | |
predicate | examples |
abort | abort |
halt | halt. |
Terms | |
predicate | examples |
var(?Term) | |
nonvar(?Term) | |
?Term1 = ?Term2 | |
functor(?Term, ?Name, ?Arity) | |
?Term =.. ?List | |
length(?List, ?Length) | |
name(?Atom, ?Chars) | |
Term Comparison | |
predicate | examples |
?Term1 ?Term2 | |
?Term1 ?Term2 | |
Arithmetic | |
predicate | examples |
?Varis+Expression | |
Arithmetic Operators | |
operator(s) (infix predicate name) | meaning |
+ - * | overloaded floating point and integer operations |
/ // | floating point and integer division, respectively |
remmod | remainder and modulo operations, respectively |
Arithmetic relational Operators | |
operator(s) (infix predicate name) | meaning |
< =< > >= =:= | relationships between two numeric expressions |
Input/Output | |
predicate | examples |
read(-Term) | |
write(?Term) | |
I/O of Sentences extensions necessary for parsing natural language constructs. | |
predicate | notes |
read_sentence(-ListOfAtoms) | May have to be modified to handle special lexical constraints. |
write_sentence(+ListOfAtoms) | |
Clause Database | |
predicate | examples |
assert(+Clause) | |
asserta(+Clause) | note: extra parentheses required for clause with a body. This will be the most usual version for our purposes. |
assertz(+Clause) | Add assertion at bottom of search path - will be found last. |
clause(+Head,?Body) | |
retract(+Clause) | Retract the clause matching Clause |
retractall(+Head) | Retract all clauses with head matching Head E.g., retractall(stuff(_)). erases all clauses of predicate stuff/1 |
abolish(+Predicates) | Abolish all clauses with head matching any in the list Predicates E.g., abolish(stuff/1). erases all clauses of predicate stuff/1 and makes predicate stuff/1 unknown |
Sets and Bags | |
predicate | examples |
setof(?Template,+Generator,-Set) | |
member(+Term,+List) | |
nonmember(+Term,+List) | |
make_set(+List1,-List2) | Remove duplicates from List1. |
delete(+Term,+List1,-List2) | Remove item Term from List1. |
insert(+Term,+List1,-List2) | Add item Term to List1. |
subset(+List1,+List2) | Is List1 a subset of List2? |
equal(+List1,+List2) | Do List1 and List2 have same elements? |
difference(+List1,+List2,-List3) | Remove items in List2 from List1 (set difference). |
union(+List1,+List2,-List3) | Add items in List2 to List1 (set union). |
append(+List1,+List2,-List3) | Add items in List2 to List1 (duplicates are presered). |
nth(+List,+Number,+Term) | Get nth item in a list. |
Useful extensions to the built-in predicates. (source code) |
Useful Extensions.(Some Prolog implementations have most of these built-in.)
Under construction ...
Note, most of these are built-in to our versionof Prolog and you should NOT re-write them. Test first!