CS252: Systems Programming Ninghui Li Program Interview Questions.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

Chapter 6 Structures By C. Shing ITEC Dept Radford University.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
CS50 WEEK 6 Kenny Yu.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
CS Data Structures II Review COSC 2006 April 14, 2017
Recursion CSC 220: Data Structure Winter Introduction A programming technique in which a function calls itself. One of the most effective techniques.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Brought to you by Max (ICQ: TEL: ) February 5, 2005 Advanced Data Structures Introduction.
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Chapter 9 (modified) Abstract Data Types and Algorithms Nell Dale John Lewis.
Data structures Abstract data types Java classes for Data structures and ADTs.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
Data Structures Systems Programming. Systems Programming: Data Structures 2 2 Systems Programming: 2 Data Structures  Queues –Queuing System Models –Queue.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Advanced Data Structure By Kayman 21 Jan Outline Review of some data structures Array Linked List Sorted Array New stuff 3 of the most important.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Linear Data Structures
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Data Structure and Algorithms
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Data Structures and Algorithms Lists, Stacks, Queues, and Graphs Sorting and searching algorithms.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture3.
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
Week 15 – Monday.  What did we talk about last time?  Tries.
Advanced Sorting 7 2  9 4   2   4   7
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
COMP261 Lecture 23 B Trees.
CSE373: Data Structures & Algorithms
Data Structures Using C, 2e
Queues.
Computing with C# and the .NET Framework
Chapter 12 – Data Structures
COSC160: Data Structures: Lists and Queues
Top 50 Data Structures Interview Questions
Data Structure Interview Question and Answers
Chapter 15 Lists Objectives
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Stack and Queue APURBO DATTA.
CSC 172– Data Structures and Algorithms
Cse 373 April 26th – Exam Review.
Introduction to Data Structure
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Lesson Objectives Aims
CS6045: Advanced Algorithms
DATA STRUCTURE.
EE 312 Final Exam Review.
Lecture 13: Computer Memory
CSCS-200 Data Structure and Algorithms
Presentation transcript:

CS252: Systems Programming Ninghui Li Program Interview Questions

String Problem Input is a string containing a set of words separated by white space(s). Write a function that will transform it to a string in which the words appear in the reverse order. For example "this is really good" becomes "good really is this". We require that only a constant amount of extra space, and linear time is used.

String Problem Solution Solution: two-pass processing First pass reverse whole string Second pass, find each word and reverse it locally

Similar Problem Consider strings over alphabet {a,b,c,d}, write a function that takes an input string, and (1) removes each “b”, (2) replaces each “a” by “dd”, and does so in place (assume the string is in a buffer long enough for the result) using constant additional storage.

Linked List: Deletion from a Singly Linked List Given a singly linked list, you are given one node and are asked to delete it from the list. You are asked to do it in constant time. What is the challenge? This is possible when the node is not the last node in the list, and copying the data field in the node takes constant time. How?

Linked List: Checking for Cyclicity Although a linked list is supposed to end with NULL, it is possible to have it points to another element in the list, creating a cycle Write code to check whether a singly linked list ends in NULL or contains a cycle C1: no modifying the list. C2: constant extra space. C3: linear time running time How to find the first element of the cycle?

Linked List: Checking for Cyclicity Solution 1: Satisfies C2, C3; violate C1; Remember the head, reverse the linked list as one traverses it, and report cycle if one reaches the head node again. Solution 2: Satisfies C2, C3; violates C1; assumes that there is a bit in the data structure that is initialized to 0

Linked List: Checking for Cyclicity Solution 3: Satisfies C1, C3; violate C2; Record all visiting nodes in a hash table, and do a lookup for each newly encountered node. Solution 4: Satisfies C1, C2; violates C3; For each newly encountered node, check whether its next will be visited if one starts from head again and try to traverse to current node.

Linked List: Checking for Cyclicity Solution 5: Satisfies C1, C2, C3; Uses 2 pointers. One fast and one slow. Fast one advances two steps each time, and show one advances one step each time, they meet if and only if there is a cycle. To figure out where cycle starts, First figure out cycle length X, and pinning fast pointer, and count how many steps it takes slow pointer to meet fast Then set fast pointer at position X, and slow at beginning, advance both at 1 step at a time, when they meet, slow is pointing at beginning of cycle

Additional LinkedList Problem How to find the middle of a linked list in one pass of the linked list?

Tower of Hanoi Move n rings stacked on Peg 1 (rings are from small to large from top to bottom) to Peg 2, Peg 3 is empty. Write a program to output the necessary steps to move n rings on Peg 1 to Peg 2.

Implementing a Queue with two Stacks Queue (FIFO) supports two operation enqueue (add an element) dequeue (remove the first added element) Stack (LIFO) supports two operations push (add an element) pop (remove the last added element) How to implement a queue using two stacks?

Implementing a Queue with two Stacks Enqueue(e) { stack1.push(e); } Dequeue(){ if (stack2.isEmpty()) { while(! stack1.isEmpty()) { stack2.push(stack1.pop()); } return stack2.pop(); }

Implement a Reentrant (or Recursive) Lock If a thread currently holding the lock tries to lock it again, for a non-reentrant or non- recursive lock, we have a deadlock A reentrant (or recursive) lock allows the thread to proceed; the thread must unlock it correct number of times for other process to obtain the lock

Big Data with Limited Memory Given an input file with four billion non- negative integers, provide an algorithm to generate an integer which is not contained in the file. Assume you have 1GB of memory available for this task. Use a bit vector, each bit to represent whether an integer is present; this requires 2^31 bit, or about 256M bytes. Taken from Gayle Laakmann McDowell: “Cracking the Coding Interview”, 5 th Edition.

Big Data with Memory Limit (continued) What if you have only 10MB of memory? Assume that for input you have no more than one billion non-negative integers, and they are all distinct. First scan determines # of integers in each thousands, e.g., between 0 and 1023, 1024 and 2047, etc. Find a region that is not full, second scan finds a number in that region What if the integers are not guaranteed to be distinct? Taken from Gayle Laakmann McDowell: “Cracking the Coding Interview”, 5 th Edition.

Hash Table How to design a hash function for words in a dictionary, e.g., for use in a hash table of size N? What makes a good hash function? Distribution of output is close of uniform over [0..N-1]. Is sum of all characters a good hash function? Is product of all characters a good hash function?

Anagrams Two words are anagrams if they contain the same set of characters. Write a program to partition a dictionary into sets of words such that all words in each set are anagrams.

Divide and Conquer Recursively Divide: break down the problem into two or more sub problems: Conquer: solve the sub problems, and then combine their results Example: Merge sort

Divide and Conquer Question Compute the number of reversed pairs in an array A of numbers. A pair (i,j) is reversed if A[i]>A[j]. Solution idea (do merge sort while computing this): # of inverse in whole = # of inverse in left half + # of inverse in right half + # of inverse involving one element in left and one in right (can be computed while merging left and right) Code available versions.java

Divide and Conquer Question Diameter of a tree Consider a tree where each edge has a number (distance), compute the maximal distance between two leaf nodes. Solution Idea: For each internal node, computes the max distance as well as the max height

Pseudo Code max_dist(node *p, &dt, &ht) {// dt:distance, ht:height int cd, ch, ht2=0, nht;// ht2 stores second highest height *dt=0; *ht=0; if (p is leaf) return; for each child c of p { max_dist(c, &cd, &ch); if (cd>*dt) *dt=cd; // current max distance is from subtree at c nht = ch + e(c); // e(c) is distance on the edge to child c if (nht > *ht) { ht2 = *ht; *ht = nht;} else if (nht > ht2) { ht2 = nht; } if (ht2>0 && *ht+ht2>*dt) { *dt = *ht+ht2; } // current max distance from leafs in two subtrees }