Git Product home page Git Product logo

ctci_example_solution's Introduction

Eaxmples and Solutions

1. Sorting and Searching

Quick Sort

  • randomly choose a pivot, seperate the array into LEFT and RIGHT

  • items which are less than pivot, put in LEFT

  • items which are greater than pivot, put in RIGHT

  • Quick sort LEFT

  • Quick sort RIGHT

    Quick Sort

Merge Sort

  • If only one item, DONE;

  • Separate original array into two part, LEFT(0 ~ mid), RIGHT(mid ~ end)

  • Merge Sort LEFT

  • Merge Sort RIGHT

    // right now, LEFT and RIGHT are both sorted

  • compare the first uncompared the items from LEFT and RIGHT, pick the small one insert to the result array.

  • There should be remaining items in LEFT or RIGHT, put all of them in the end of result array.

Binary Search

Easy but useful algorithm. Searching target value from a sorted array

  • Compare target with mid of the array
  • if equals. DONE
  • if less. Binary Search left part.
  • if greater. Binary Search right part.

Interview Questions

  1.   Sorted Merge: Given two sorted arrays, A and B, where A has a large enough buffer at the

    end to hold B. Write a method to merge B into A in sorted order.

    • 10.1 Solution

      Comparing the last element of A and B, choose the larger one and insert it to the end of A

      10.1 code

  2.   Group Anagrams: Write a method to sort an array of strings so that all the anagrams are next to

    each other.

    • 10.2 Solution

      Anagrams are words or phrases share the same characters but not in the same order.

      What we need to do is group up the anagrams.

      How to make sure if two words are anagrams?

      Check characters and their occurrences.

      How to group up?

      Use a hashmap <String, List>, where the list is the group, the String is an alphabetical String to represent anagrams

      10.2 code

ctci_example_solution's People

Contributors

macknever avatar

Watchers

 avatar

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.