Coder Social home page Coder Social logo

data_structures's Introduction

Interview Prep

For interview questions and what to ask, check out this doc. Otherwise, keep reading for basic data structures, sorting algorithms, and time complexities. I'll add a section on technical tips too later.


Data Structures

BINARY SEARCH TREE

Useful for:

Sorting data and everything, including rebalancing, can be done in O(log n).

Notes:

Generally use unique values (don't want duplicates).

Time Complexity:

Operation Average Worst
Insert O(log n) O(n)
Remove O(log n) O(n)
Search O(log n) O(n)

ARRAYS

Useful for:

Storing and accessing sequential data, temporarily storing, as an IO buffer (when reading from a file), lookup table, and hack for multiple return values.

Notes:

Can implement a dynamic arrays from static ones by continuously allocating double the space once the array is full.

Time Complexity:

Operation Average Worst
Access O(1) O(1)
Append --- O(1)
Insert --- O(n)
Remove --- O(n)
Search O(n) O(n)

LINKED-LISTS

Useful for:

Useful in lists, queues, and stacks. Often used for circular (round robin) or adjacency lists.

Notes:

Singly-linked will save up on space and it's simpler to implement but it can make it more difficult to access previous nodes. Doubly-linked takes up double the space since we have 2x the pointers but easier to traverse backwards.

Time Complexity:

Operation Average Worst
Insert (T/H) O(1) O(1)
Insert O(n) O(n)
Remove O(n) O(n)
Search O(n) O(n)

STACKS

Useful for:

Useful for undo functions, checking for matching braces, other syntax (or TMs), supports recursion behind the scenes, and is used in DFS on a graph.

Notes:

Has 2 primary operations: push and pop. Last in, first out: what you most recently pushed will be what comes out when you pop.

Time Complexity:

Operation Average Worst
Push O(1) O(1)
Pop O(1) O(1)
Peek/Top O(1) O(1)
Search O(n) O(n)

QUEUES

Useful for:

Used to model a real-world queue like standing in line, a sequence of elements, web server manager for first come, first serve, or BFS graph traversal.

Notes:

It's a linear data structure with has 2 main operations: dequeue ("polling") and enqueue ("offering"). You remove elements from the front and add elements to the back. Often done via singly-linked list since arrays (static) might not be big enough.

Time Complexity:

Operation Average Worst
Enqueue O(1) O(1)
Dequeue O(1) O(1)
Peek/Top O(1) O(1)
Contains O(n) O(n)
Remove O(n) O(n)

HASH MAP

Useful for:

Helpful for tracking item frequencies, if large files have equal contents (without opening files),

Notes:

Provides a mapping from keys to values via hashing (called a "key-value" pair where keys are unique). Keys are immutable.

Collision Resolution Methods:

  1. Open Addressing: Find another place within hash table by using an offset from original hash.

  2. Separate Chaining: Use a separate data structure like a list to hold all different values (key-value pairs).

Time Complexity:

Operation Average Worst
Lookup O(1) O(n)
Remove O(1) O(n)
Insert O(1) O(n)

Sorting Algorithms

Insertion Sort

Useful for:

...

Time Complexity:

Best Average Worst Space
O(n) O(n^2) O(n^2) O(1)

Selection Sort

Useful for:

...

Time Complexity:

Best Average Worst Space
O(n^2) O(n^2) O(n^2) O(1)

Merge Sort

Useful for:

Ideal for combining lists.

Time Complexity:

Best Average Worst Space
O(nlogn) O(nlogn) O(nlogn) O(n)

Quick Sort

Useful for:

...

Time Complexity:

Best Average Worst Space
O(nlogn) O(nlogn) O(n^2) O(logn)

Heap Sort

Useful for:

Can be chosen over quick sort because, in the case of a partially sorted array, it's worst case will always be O(nlogn) not O(n^2).

Time Complexity:

Best Average Worst Space
O(n) O(nlogn) O(nlogn) O(1)

Other Notes

BFS Graph:

Start at a node, add its neighbors to queue, and visit their neighbors (add to queue). We know if all nodes have been visited by marking them as visited (bool).

data_structures's People

Contributors

ntrappe avatar

Watchers

 avatar

Forkers

yayka

data_structures's Issues

January Updates

  • Maple Tree (BST) file
    • insert into a maple tree
    • remove from a maple tree
    • DFS
    • BFS
    • dynamic: is it possible to re-order leaves such that the descendants of a leaf are no lighter than it?
    • remove by color
  • LinkedList
    • reverse a linked list
    • reverse by group
      • error: seg fault
      • test edge cases, normal ok (not 1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.