Thursday, October 27, 2016

Valid Parentheses

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not. First of all, if this string has an odd number of characters, we know already that it isn't valid. class Solution(object): def isValid(self, s): if len(s) % 2 ==...

Wednesday, October 26, 2016

Remove Nth Node from End of List

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n will always be valid. Try to do this in one pass. We are given the ListNode class class ListNode(object): def __init__(self, x): ...

Sunday, October 23, 2016

ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: P   A    H   N A P L S  I  I  G Y       I      R And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows: string convert(string text, int nRows); convert("PAYPALISHIRING",...

Saturday, October 22, 2016

Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] :type target:...

Wednesday, September 14, 2016

Three in One - Part One

Describe how you could use a single array to implement three stacks.Hint 1. A stack is simply a data structure in which the most recently added elements are removed first. Can you simulate a single stack using an array? Remember there are many possible solutions, and there are tradeoffs of each.Hint 2. We could simulate three stacks in an array by just allocating the first third of the array to the...

Monday, September 12, 2016

Return Kth to Last

Implement an algorithm to find the kth to last element of a singly linked list.If we knew the length of the linked list, then we could just subtract k and we would move a pointer length-k times.Here, I think the best way would be to just iterate through the linked list with two pointers. The two nodes start on the head of the linked list. The first pointer would go ahead k steps, and then both would...

Thursday, August 18, 2016

Binary Search

Binary search is one the simplest search algorithms(most are pretty simple) to implement. However, first it assumes an important property of an array that isn't always true; the array is sorted. This search is based on the number of elements(length), and we fold the array in half each time, which makes search time logarithmic.First, you see in the...

Sunday, April 17, 2016

Relational Algebra - An Intro of it's Use and Operators

Consider the following database scenario:You are planning a high school graduation party for the school. You keep data on those who have registered to be present on that day. We have students(S), teachers(T), staff members(SM), parents(P), and siblings(C) of the students, followed by relatives(F), and a list of registered people for the party(R). We...

Friday, March 11, 2016

MySQL Queries and commands

In this post I hope to go through some MySQL commands that would help users get started and used to using it. Remember that semicolons are default used to show end of command/queries.Creation/Deletion CommandsUSE <database name>:This is used to enter an existing database.CREATE DATABASE <database name>:This is used to create a database.CREATE...

Monday, February 29, 2016

Breadth-First Search

Breadth-first search is a search algorithm to find something in an tree. This search algorithm utilizes a queue to keep track of all the nodes that the algorithm will explore. If you're not sure what a queue is then you should take a look at it here.What basically happens:The search algorithm starts at the root node(A), and looks at each node...

Friday, February 26, 2016

Depth-First Search

Depth-first search is a search algorithm to find something in an tree. This search algorithm utilizes a stack to keep track of all the nodes that the algorithm will explore. If you're not sure what a stack is then you can read about it here.What basically happens:The search starts at the root node(A), and continues to travel down until it reaches a...

Friday, February 5, 2016

Database Concepts - A Short Post about the Relational Database Model

via WikipediaRelational Database Model:The relational database model, different from the entity relation model, shows specific data and information about a database table. A relational database model contains two parts: An instance, and a Schema.Schema:The schema is specific name of the relation, or the name of each column. If you think back to the...

Wednesday, February 3, 2016

Setting Up MySQL

Hi Everyone!I know I haven't been posting much these past few months, I was busy with many other things and I decided to take a breather with blogging. However, things have cleared out of my way now, and I will start again with posts.Installing:For Linux users, type the following into your command line:shell>sudo apt-get install mysql-servershell>sudo...