Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 357
Good morning! Here's your coding interview problem for today.

This problem was asked by LinkedIn.

You are given a binary tree in a peculiar string representation. Each node is written in the form `(lr)`, where `l` corresponds to the left child and `r` corresponds to the right child.

If either `l` or `r` is `null`, it will be represented as a zero. Otherwise, it will be represented by a new `(lr)` pair.

Here are a few examples:

* A root node with no children: `(00)`
* A root node with two children: `((00)(00))`
* An unbalanced tree with three consecutive left children: `((((00)0)0)0)`

Given this representation, determine the depth of the tree.

Pull request description:
Day357
Resolves #706.

Patch:
diff --git a/README.md b/README.md
index 19daf0e..6117477 100644
--- a/README.md
+++ b/README.md
@@ -343,6 +343,7 @@ problems from
 * [Day 353](https://github.com/vaskoz/dailycodingproblem-go/issues/701)
 * [Day 355](https://github.com/vaskoz/dailycodingproblem-go/issues/704)
 * [Day 356](https://github.com/vaskoz/dailycodingproblem-go/issues/705)
+* [Day 357](https://github.com/vaskoz/dailycodingproblem-go/issues/706)
 * [Day 359](https://github.com/vaskoz/dailycodingproblem-go/issues/708)
 * [Day 360](https://github.com/vaskoz/dailycodingproblem-go/issues/709)
 * [Day 361](https://github.com/vaskoz/dailycodingproblem-go/issues/712)
diff --git a/day357/problem.go b/day357/problem.go
new file mode 100644
index 0000000..b3157c8
--- /dev/null
+++ b/day357/problem.go
@@ -0,0 +1,24 @@
+package day357
+
+// StringTreeDepth returns the height of the binary tree
+// using the problem stated notation for the tree.
+// Runs in O(N) time where N is the length of the input string
+//  and O(1) space.
+func StringTreeDepth(tree string) int {
+	var maxStreak, current int
+
+	for _, r := range tree {
+		switch r {
+		case '(':
+			current++
+		case ')':
+			current--
+		}
+
+		if current > maxStreak {
+			maxStreak = current
+		}
+	}
+
+	return maxStreak
+}


Test patch:
diff --git a/day357/problem_test.go b/day357/problem_test.go
new file mode 100644
index 0000000..3bd7051
--- /dev/null
+++ b/day357/problem_test.go
@@ -0,0 +1,34 @@
+package day357
+
+import "testing"
+
+// nolint
+var testcases = []struct {
+	tree  string
+	depth int
+}{
+	{"(00)", 1},
+	{"((00)(00))", 2},
+	{"((((00)0)0)0)", 4},
+	{"((0((00)0))(00))", 4},
+	{"(0(0(0(0(0(00))))))", 6},
+	{"(((0(((00)(00))((00)(00))))0)(0((((0(00))0)0)(00))))", 7},
+}
+
+func TestStringTreeDepth(t *testing.T) {
+	t.Parallel()
+
+	for _, tc := range testcases {
+		if depth := StringTreeDepth(tc.tree); depth != tc.depth {
+			t.Errorf("For tree %s, expected %v, got %v", tc.tree, tc.depth, depth)
+		}
+	}
+}
+
+func BenchmarkStringTreeDepth(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			StringTreeDepth(tc.tree)
+		}
+	}
+}