avatarJen-Hsuan Hsieh (Sean)

Summary

The undefined website presents a comprehensive overview of the Algorithms, Part 1 course from Princeton, focusing on the implementation and application of stacks, queues, deques, and randomized queues, with a particular emphasis on the Week 2 programming assignment.

Abstract

The article on the undefined website delves into the Algorithms, Part 1 course offered by Princeton University, which is available on Coursera. It is tailored for IT and software engineering professionals who aim to solidify their understanding of algorithms. The course is structured into weekly segments, with the article specifically focusing on the data structures stack and queue, their various implementations, and their practical applications. The author, Sean, shares his insights on the separation of interface and implementation, provides detailed explanations of stack and queue APIs, and discusses different implementation strategies using arrays and linked lists. The article also includes Java code snippets from the official course videos and challenges readers with related LeetCode problems. A significant portion of the article is dedicated to the Week 2 programming assignment, which involves implementing deques and randomized queues, with the latter requiring the removal of a random item from the data structure. Sean offers his solutions in JavaScript and invites feedback from readers. The article concludes with a summary, a call to subscribe to the author's Medium page, and an introduction to the author's side project, Daily Learning.

Opinions

  • The author emphasizes the importance of separating the interface from the implementation in data structures, allowing clients to choose from various implementations without altering their code.
  • Sean suggests that understanding the fundamental data structures like stacks and queues is crucial for software engineers and IT professionals.
  • The article provides a comparative analysis of resizing array and linked list implementations for stacks and queues, highlighting the trade-offs in terms of performance and complexity.
  • The author values practical learning, encouraging readers to tackle algorithmic problems on platforms like LeetCode to reinforce their understanding.
  • By sharing his own solutions to the Week 2 programming assignment, Sean advocates for a hands-on approach to learning and encourages peer review and collaboration within the community.
  • The inclusion of his personal LinkedIn profile, Medium membership link, and Facebook page for his side project indicates the author's commitment to community engagement and continuous learning.

Algorithms, Part 1 Course from Princeton — Stack, Queue, and Week 2 Assignment Deques and Randomized Queues

Introduction

Algorithms, Part 1 is a solid course for IT or software engineers to learn algorithms which are provided by Princeton. Of course, we still have to take time to clarify the concepts after completing the class.

Stack and queue are fundamental data structure for storing data in the practice. In this article, we will focus on APIs and different ways for implementations.

About this Series

This series aims to wrap up contents of Algorithms, Part 1.

Agenda

It includes the following topics in this article.

1. Separate interface and implementation

What we want to do is to completely separate the details of the implementation from the client

e.g., stack, queue, bag, priority queue, symbol table, union-find, etc.

Roles

  • client : program using operations defined in an interface
  • implementation : actual code implementing operations
  • interface : the description of data type, basic operations

Benefits

  1. Client has many implementation from which to choose but the client code should only perform the basic operations
  2. Many clients can re-use the sameimplementation

2. Introduction to Stack

Last in First out (LIFO): remove and return the item most recently added

APIs

APIs we have to implement for the stack.

Resizing array implementation vs linked list implementation

Stack can be implemented with either resizing array or linked list. client can use interchangeably.

Related LeetCode questions

3. Stack implementation in Linked-list

Description

  • Maintain pointer to first node in a linked list
  • Insert and remove the front
source: https://readmedium.com/learning-data-structure-in-javascript-for-beginners-f5752363a4e1#bf9b

Propositions

Every operations takes constant time in the worst case

Implementation in Java (from the official video)

4. Stack implementation in Array (Resizing array)

Description

  • Use array s[] to store N items on stack
  • push : add new item at s[N]
  • pop : remove new item at s[N — 1]

Considerations

  • underflow: throw exception if pop from an empty stack
  • overflow: use resizing array for array implementation

Resizing array

  • If the array is full, create a new array of twice the size, and copy items
  • consequence: inserting first N items takes time proportional to N (not N ^ 2)
  • If the array is full, having size of array s[]

Too expensive in worst case for resizing array

  • consider push-pop-push-pop…sequence when array is full
  • each operation takes time proportional to N

Proposition

Implementation in Java (from the official video)

Related LeetCode questions

5. Introduction to Queue

First in first out (FIFO): remove and return the item least recently added

APIs

APIs we have to implement for the queue.

6. Queue implementation in linked-list

Description

  • Maintain pointer to first and last nodes in a linked list
  • Insert the front
  • Remove from the end
source: https://readmedium.com/learning-data-structure-in-javascript-for-beginners-f5752363a4e1

Implementation in Java (from the official video)

7. Queue implementation in Array (Resizing array)

Description

  • Use array q[] to store items in queue
  • enqueue : add new item at q[tail]
  • dequeue : remove item from q[head]
  • Update head and tail modulo the capacity

Implementation in Java (completed by myself, from the official video)

8. Priority queue implementation in array

Description

  • Queue: remove the item most recently added
  • Stack: remove the item least recently added
  • Randomized queue: remove a random item
  • Priority queue: remove the largest (or smallest) item

APIs

Implementation in Java (from the official video)

  • Unordered array implementation

Challenge

  • Implement all operations efficiently

9. Completed code in JavaScript

  • completed by myself, not from the official video

Week 2 Programming Assignment: Deques and Randomized Queues

There are two requirements.

  1. deque: double-ended queue that supports adding and removing items from either the front or the back of the data structute
  2. randomized queue: the item removed is chosen uniformly at random among items in the data structure
  3. client program: read a sequence of string and print exactly k of item, uniformly at random

Requirements in detail

Solutions

  • Deque.java
  • RandomizedQueue.java
  • Permutation.java

Grades

Summary

Thanks for your patient. I am Sean. I work as a software engineer.

This article is my note. Please feel free to give me advice if any mistakes. I am looking forward to your feedback.

  • Subscribe me
  • The Facebook page for articles
  • The latest side project: Daily Learning
Software Development
Stack
Deque
Queue
Data Structures
Recommended from ReadMedium