My Interview Prep Flashcards
Java 7 I/O
//Create a new Path Path newFile = Paths.get("test1"); try { Files.deleteIfExists(newFile); newFile = Files.createFile(newFile); } catch (IOException ex) { System.out.println("Error creating file"); } System.out.println(Files.exists(newFile));
//Writing to file4 try(BufferedWriter writer = Files.newBufferedWriter( newFile, Charset.defaultCharset())){ writer.append("This is first line"); writer.newLine(); writer.flush(); }catch(IOException exception){ System.out.println("Error writing to file"); }
Bit manipulation
1=negative 0=positive 1&&1=1 1&&0=0 0&&0=0 1||1=1 1||0=1 0||0=0 0^0=0 (XOR) 0^1=1 1^1=0
tree traversal depth-first search
In depth-first order, we always attempt to visit the node farthest from the root node that we can, but with the caveat that it must be a child of a node we have already visited. Unlike a depth-first search on graphs, there is no need to remember all the nodes we have visited, because a tree cannot contain cycles.
tree traversal breadth-first search
level-order traversal
always attempts to visit the node closest to the root that it has not already visited.
Binary tree
tree where each node has no more than 2 children
array list vs linked list
Arrays have O(1) random access, but are really expensive to add stuff onto or remove stuff from.
Linked lists are really cheap to add or remove items anywhere and to iterate, but random access is O(n).
vector vs array list
You should normally use ArrayList - it offers better performance.
Vector has just one “advantage” - it is synchronised for concurrent modification. But it turns out that this feature isn’t very useful - if you are writing concurrent code, you typically need to lock at a much higher level of granularity than an individual collection class.
As a result, Vector is often considered deprecated nowadays.
hash map vs hash table
There are several differences between HashMap and Hashtable in Java:
Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.
Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.
One of HashMap’s subclasses is LinkedHashMap, so in the event that you’d want predictable iteration order (which is insertion order by default), you could easily swap out the HashMap for a LinkedHashMap. This wouldn’t be as easy if you were using Hashtable.
Hash tables
n computing, a hash table (also hash map) is a data structure used to implement an associative array, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the correct value can be found.
the hashing part is to transform a large space (of arbitrary length, usually strings, etc) and mapping it to a small space (of known size, usually numbers) for indexing.
Real World Example:
Hash & Co., founded in 1803 and lacking any computer technology had a total of 300 filing cabinets to keep the detailed information (the records) for their approximately 30,000 clients. Each file folder were clearly identified with its unique number from 0 to 299.
The filing clerks of that time had to quickly fetch and store client records for the working staff. The staff had decided that it would be more efficient to use a hashing methodology to store and retrieve their records.
To file a client record, filing clerks would use the unique client number written on the folder. Using this client number, they would modulate it by 300 (the hash key) in order to identify the filing cabinet it is contained in. When they opened the filing cabinet they would discover that it contained many folders ordered by client number. After identifying the correct location, they would simply slip it in.
Java primitives
n=# of bits
2^n-1 to
-2^(n-1) - 1
byte 8 bit
-128 to 127
short 16 bit int 32 bit long 64 bit float 32 bit double 64 bit boolean 1 bit char single 16-bit Unicode character
Merge sort
An example of merge sort. First divide the list into the smallest unit (1 element), then compare each element with the adjacent list to sort and merge the two adjacent lists. Finally all the elements are sorted and merged. avg O(n log(n))
Big O notation
Big O notation is used in Computer Science to describe the performance or complexity of an algorithm. Big O specifically describes the worst-case scenario, and can be used to describe the execution time required or the space used (e.g. in memory or on disk) by an algorithm.
O(1) describes an algorithm that will always execute in the same time (or space) regardless of the size of the input data set
O(N) describes an algorithm whose performance will grow linearly and in direct proportion to the size of the input data set
O(N2) represents an algorithm whose performance is directly proportional to the square of the size of the input data set. (ie 2 iterative for loops)
O(2N) denotes an algorithm whose growth will double with each additional element in the input data set. The execution time of an O(2N) function will quickly become very large
O(log N). The iterative halving of data sets described in the binary search example produces a growth curve that peaks at the beginning and slowly flattens out as the size of the data sets increments
NoSQL
A NoSQL database provides a mechanism for storage and retrieval of data that is modeled in means other than the tabular relations used in relational databases. Motivations for this approach include simplicity of design, horizontal scaling and finer control over availability. The data structure (e.g., tree, graph, key-value) differs from the RDBMS, and therefore some operations are faster in NoSQL and some in RDBMS
SocketPC (NetSpend)
custom partner connection service which acts as a client to NEO. SocketPC receives XML-like messages on a direct socket connection, parses them, and then invokes NEO
A socket is a software endpoint that establishes bidirectional communication between a server program and one or more client programs. The socket associates the server program with a specific hardware port on the machine where it runs so any client program anywhere in the network with a socket associated with that same port can communicate with the server program.
Java generics
List (String part the generic)
Quick sort
Quicksort is a divide and conquer algorithm. Quicksort first divides a large list into two smaller sub-lists: the low elements and the high elements. Quicksort can then recursively sort the sub-lists.
The steps are:
Pick an element, called a pivot, from the list.
Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
Recursively apply the above steps to the sub-list of elements with smaller values and separately to the sub-list of elements with greater values.
The base case of the recursion is lists of size zero or one, which never need to be sorted.
avg O(n log(n))
Hungarian folk dance sort YouTube!
PaWS
The Partner Web Services (PaWS) enables larger partners to integrate their own POS systems with the Netspend system. Creates a war file that implements CXF web services using SOAP XML that is then deployed in a web service container such as tomcat or jetty.
Testing REST services
rest-assured Java DSL (domain specific language) for easy testing of REST services
REST clients
http clients for testing REST services:
Postman
REST Console
Restrictions on generics
Cannot Instantiate Generic Types with Primitive Types
Cannot Create Instances of Type Parameters
Cannot Declare Static Fields Whose Types are Type Parameters
Cannot Use Casts or instanceof With Parameterized Types
Cannot Create Arrays of Parameterized Types
Cannot Create, Catch, or Throw Objects of Parameterized Types
Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type
Generics type erasure
Generics were introduced to the Java language to provide tighter type checks at compile time and to support generic programming. To implement generics, the Java compiler applies type erasure to:
Replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods.
Insert type casts if necessary to preserve type safety.
Generate bridge methods to preserve polymorphism in extended generic types.
Type erasure ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead.
NEO
NetSpend Enterprise Objects - middle business-logic tier for the 3-Tier NetSpend application.
Neo is a collection of data objects and Enterprise Session Beans which run on the NetSpend production server.
NEO is structured using the following discrete layers:
Client Session
EJB
Model
NEO DAO
Types of linked lists
Singly-linked lists work by having each node pointing to the next node, with the tail node (or end node) pointing to nothing (or a null reference). Doubly-linked lists expand upon the singly-linked list, with each node pointing to the previous node as well as the next node. In doubly-linked lists, both the head node (first node) and the tail node point to nothing. Circularly-linked lists expand upon the singly-linked list differently than the doubly-linked list; instead of having the tail node pointing to nothing, it “circles” back around, pointing to the head element as the name suggests.
Queues
A queue is a container of objects (a linear collection) that are inserted and removed according to the first-in first-out (FIFO) principle. An excellent example of a queue is a line of students in the food court of the UC. New additions to a line made to the back of the queue, while removal (or serving) happens in the front. In the queue only two operations are allowed enqueue and dequeue. Enqueue means to insert an item into the back of the queue, dequeue means removing the front item
" + title + "
"); // Prints "Hello, world!" // Set a hyperlink image to refresh this page out.println("