Browse Source

Initial commit

master
Martins Eglitis 10 months ago
commit
1fa725f598
15 changed files with 431 additions and 0 deletions
  1. +8
    -0
      .idea/google.iml
  2. +6
    -0
      .idea/misc.xml
  3. +8
    -0
      .idea/modules.xml
  4. +53
    -0
      .idea/workspace.xml
  5. +33
    -0
      ctci.md
  6. +43
    -0
      hints.txt
  7. +1
    -0
      src/main.go
  8. +17
    -0
      src/q1.go
  9. +33
    -0
      src/q1_test.go
  10. +25
    -0
      src/q2.go
  11. +43
    -0
      src/q2_test.go
  12. +17
    -0
      src/q3.go
  13. +41
    -0
      src/q3_test.go
  14. +58
    -0
      src/q4.go
  15. +45
    -0
      src/q4_test.go

+ 8
- 0
.idea/google.iml View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

+ 6
- 0
.idea/misc.xml View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>

+ 8
- 0
.idea/modules.xml View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/google.iml" filepath="$PROJECT_DIR$/.idea/google.iml" />
</modules>
</component>
</project>

+ 53
- 0
.idea/workspace.xml View File

@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="32480d2b-24e3-4802-8050-1e5c87119c4a" name="Default Changelist" comment="" />
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Go File" />
</list>
</option>
</component>
<component name="GOROOT" path="/usr/lib/go" />
<component name="ProjectId" id="1T7mrcEX6sywinwvK2Y7ppwq6nW" />
<component name="PropertiesComponent">
<property name="DefaultGoTemplateProperty" value="Go File" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="go.import.settings.migrated" value="true" />
<property name="go.sdk.automatically.set" value="true" />
<property name="last_opened_file_path" value="$USER_HOME$" />
<property name="node.js.detected.package.eslint" value="true" />
<property name="node.js.detected.package.tslint" value="true" />
<property name="node.js.path.for.package.eslint" value="project" />
<property name="node.js.path.for.package.tslint" value="project" />
<property name="node.js.selected.package.eslint" value="(autodetect)" />
<property name="node.js.selected.package.tslint" value="(autodetect)" />
</component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/questions" />
</key>
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
</project>

+ 33
- 0
ctci.md View File

@@ -0,0 +1,33 @@
# Cracking the Coding Interview

## I - VII
- Quick sort picks a random element as a "pivot" and then swaps values in the array such that the elements less than pivot appear before elements greater than pivot. This gives a "partial sort:'Then it recursively sorts the left and right sides using a similar process.

- How do you describe the runtime of insertion? This is a tricky question. The array could be full. If the array contains N elements, then inserting a new element will take O(N) time. You will have to create a new array of size 2N and then copy N elements over. This insertion will take O ( N) time. However, we also know that this doesn't happen very often. The vast majority of the time insertion will be in O(l) time.

- In binary search, we are looking for an example x in an N-element sorted array. We first compare x to the midpoint of the array. If x == middle, then we return. If x < middle, then we search on the left side of the array. If x > middle, then we search on the right side of the array.

- This is a good takeaway for you to have. When you see a problem where the number of elements in the
problem space gets halved each time, that will likely be a 0( log N) runtime.

- Here's a tricky one. What's the runtime of this code? Very interesting 3. example!

- The following simple code sums the values of all the nodes in a balanced binary search tree. What is its runtime? Very interesting 9. example!

- Very interesting 11. example! Not factorial but O(n) time for calculating factorial.
- Don't get fooled by naming!

- Is there anything else that is unnecessary? Yes. If there's onl one valid d value for each (a, b, c), then we can just compute it. This is just simple math: d = a + b - C • This will reduce our runtime from O(N 4 ) to O(N 3 ).

- This is another place where BCR can be useful. Any work you do that's less than or equal to the BCR is "free;' in the sense that it won't impact your runtime. You might want to eliminate it eventually, but it's not a top priority just yet.
## VIII
### 1
- A hash table is a data structure that maps keys to values for highly efficient lookup.
- If the number of collisions is very high, the worst case runtime is O(N), where N is the number of keys. However, we generally assume a good implementation that keeps collisions to a minimum, in which case the lookup time is 0(1).
- On each concatenation, a new copy of the string is created, and the two strings are copied over, character by character. The first iteration requires us to copy x characters. The second iteration requires copying 2x characters. T he third iteration requires 3x, and so on. The total time therefore is O( x + 2x + . . . + nx). This reduces toO(xn 2 ). That is because 1 + 2 + ... + n = n * (n - 1) / 2. And it happens because of the non-resizing array. Use string builders for resizable array and complexity of O(n).

+ 43
- 0
hints.txt View File

@@ -0,0 +1,43 @@
GCA points:

- take a moment, wait, before responding
- ask clarifying questions (super important to get to the right answer)
- make logic assumptions
- TALK OUT LOUD
- think about pros and cons
- measure your success

What Google is really looking for?

- Problem solving. Life at google is problem solving all day long. About asking questions, talking out loud.
- Leadership! Stay confident! It is gonna show up in your GCA answers.
- Demonstrate results with data.
- Navigating in ambiguity = move forward even if you don't know all the information.
- Flexibility, don't always stick to your answer. Be flexible, choose different paths, explain, etc.
- Don't worry if interviewer does not show up in time, be happy (WUT??)
- Prepare!
- There will be open ended questions.
- Use common sense.
- Act like you are already working at Google.
- Have honest and nice interview.
- Dress casual.
- You have to code on a whiteboard

Technical interview

Tips:
- prove on what is on your resume.
- datastructures, algorithms, time and space complexity
- code on whiteboard
- efficient code in short time frame

Focus on:
- coding, most of the time spent here
- algorithms, how complex your algo is, compare them
- the code you write must be tested!
- data structures, NP completeness, tree, tree construction, hash tables, stacks, heaps, arrays, linked lists
- RECURSION!
- semaphores, mutexes, locks, threads, etc. context switching
- system design, feature sets, api's, etc. how Internet works, etc.
- talk out loud your ideas, ask clarify questions, e.g. are there any space and time requirements
- practice

+ 1
- 0
src/main.go View File

@@ -0,0 +1 @@
package main

+ 17
- 0
src/q1.go View File

@@ -0,0 +1,17 @@
package main

func Q1(input string) bool {
m := make(map[int32]bool, 0)

for _, ch := range input {
_, ok := m[ch]
if ok {
return false
}

m[ch] = true
}

return true
}

+ 33
- 0
src/q1_test.go View File

@@ -0,0 +1,33 @@
package main

import "testing"

func TestQ1Pass(t *testing.T) {
inputs := []string{
"abc",
"AbC",
"a",
"",
"012a",
}
for _, input := range inputs {
if !Q1(input) {
t.Fail()
}
}
}

func TestQ1Fail(t *testing.T) {
inputs := []string{
"aabc",
"123010",
"a3bbbb",
}

for _, input := range inputs {
if Q1(input) {
t.Fail()
}
}
}

+ 25
- 0
src/q2.go View File

@@ -0,0 +1,25 @@
package main

func Q2(input1 string, input2 string) bool {
if len(input1) != len(input2) {
return false
}

var found bool

for _, chi := range input1 {
for _, chj := range input2 {
if chi == chj {
found = true
break
}
}

if !found {
return false
}
}

return true
}

+ 43
- 0
src/q2_test.go View File

@@ -0,0 +1,43 @@
package main

import "testing"

func TestQ2Pass(t *testing.T) {
inputs1 := []string{
"abc",
"cab",
"1a2b",
"",
}

inputs2 := []string{
"abc",
"bca",
"12ab",
"",
}

for i := range inputs1 {
if !Q2(inputs1[i], inputs2[i]) {
t.Fail()
}
}
}

func TestQ2Fail(t *testing.T) {
inputs1 := []string{
"abc",
"",
}

inputs2 := []string{
"aabc",
"a",
}

for i := range inputs1 {
if Q2(inputs1[i], inputs2[i]) {
t.Fail()
}
}
}

+ 17
- 0
src/q3.go View File

@@ -0,0 +1,17 @@
package main

func Q3(input1 string, input2 int) string {
var output string

for _, ch := range input1 {
if ch == ' ' {
output += "%20"

continue
}

output += string(ch)
}

return output
}

+ 41
- 0
src/q3_test.go View File

@@ -0,0 +1,41 @@
package main

import (
"testing"
)

func TestQ3Pass(t *testing.T) {
inputs1 := []string {
"Mr John Smith",
"Mr John",
"Mr",
"",
"Mr ",
}

inputs2 := []int {
13,
7,
2,
0,
3,
}

outputs := []string {
"Mr%20John%20Smith",
"Mr%20John",
"Mr",
"",
"Mr%20",
}

for i := range inputs1 {
if outputs[i] != Q3(inputs1[i], inputs2[i]) {
t.Fail()
}
}
}

func TestQ3Fail(t *testing.T) {

}

+ 58
- 0
src/q4.go View File

@@ -0,0 +1,58 @@
package main

import (
"strings"
)

func Q4(input string) (string, bool) {
//Basically, we need to check if every element in the map repeats
//at least 2*n times. Optionally, one and only one element might appear only once.

input = strings.ToLower(input)

m := make(map[rune]int, 0)

for _, b := range input {
if b == ' ' {
continue
}

m[b]++
}

var single string

//find which one is the single rune
for b, n := range m {
if n % 2 != 0 {
if single == "" {
single = string(b)
} else {
return "", false
}
}
}

//for more permutations, code here...
var p1 string

//TODO - maps do not produce consistent output when iterated over: https://blog.golang.org/go-maps-in-action
for b, n := range m {
//generate just two permutations, we don't need all of them...
//we can go from 0 to n / 2, then add single, then reverse the first part
for i := 0; i < n / 2; i++ {
p1 += string(b)
}
}

var p2 string

for i := len(p1) - 1; i >= 0; i-- {
p2 += string(p1[i])
}

//TODO - careful with strings - when you insert an empty string, you actually
//TODO - increase the string length

return p1 + single + p2, true
}

+ 45
- 0
src/q4_test.go View File

@@ -0,0 +1,45 @@
package main

import (
"fmt"
"testing"
)

func TestQ4Pass(t *testing.T) {
inputs := []string {
"Tact Coa",
"Mama Bee",
"Yeey",
"",
}

//outputs := []string {
// "tacocat",
// "maebeam",
// "yeey",
// "",
//}


//testing only for length...
outputs := []int {
7,
7,
4,
0,
}

for i, output := range outputs {
perm, status := Q4(inputs[i])

if len(perm) != output || !status {
fmt.Println(perm, len(perm), output)
t.Fail()
}
}
}

func TestQ4Fail(t *testing.T) {

}


Loading…
Cancel
Save