{"id":20399305,"url":"https://github.com/sunilsoni/cracking-the-coding-interview","last_synced_at":"2025-04-12T13:22:28.547Z","repository":{"id":128930379,"uuid":"106067514","full_name":"sunilsoni/cracking-the-coding-interview","owner":"sunilsoni","description":"Cracking The Coding Interview 6th Edition (CTCI6) Questions and answers-Java ","archived":false,"fork":false,"pushed_at":"2018-01-06T06:18:16.000Z","size":108,"stargazers_count":19,"open_issues_count":0,"forks_count":24,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-26T08:06:57.373Z","etag":null,"topics":["cracking-the-coding-interview","ctci","ctci-solutions","ctci6"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sunilsoni.png","metadata":{"files":{"readme":"README.adoc","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-10-07T03:02:22.000Z","updated_at":"2023-11-20T04:27:14.000Z","dependencies_parsed_at":"2023-04-18T09:18:13.959Z","dependency_job_id":null,"html_url":"https://github.com/sunilsoni/cracking-the-coding-interview","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunilsoni%2Fcracking-the-coding-interview","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunilsoni%2Fcracking-the-coding-interview/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunilsoni%2Fcracking-the-coding-interview/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunilsoni%2Fcracking-the-coding-interview/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sunilsoni","download_url":"https://codeload.github.com/sunilsoni/cracking-the-coding-interview/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248571971,"owners_count":21126555,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cracking-the-coding-interview","ctci","ctci-solutions","ctci6"],"created_at":"2024-11-15T04:28:19.531Z","updated_at":"2025-04-12T13:22:28.539Z","avatar_url":"https://github.com/sunilsoni.png","language":"Java","readme":"= Cracking the Coding Interview, 6th Edition\n\n \n\n== https://github.com/sunilsoni/cracking-the-coding-interview/tree/master/src/com/ctci6/ch01[Solutions to Chapter 1: Arrays and Strings]\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch01/IsUniqueChars.java[*Is Unique:*] Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?.\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch01/Permutation.java[*Check Permutation:*] Given two strings, write a method to decide if one is a permutation of the\nother.\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch01/URLify.java[*URLify:*] Write a method to replace all spaces in a string with '%2e: You may assume that the string has sufficient space at the end to hold the additional characters, and that you are given the \"true\" length of the string. \nNote:: If implementing in Java, please use a character array so that you can perform this operation in place.\n\n[source,java]\n-----------------\n\tEXAMPLE\n\tInput: \"Mr John Smith JJ, 13  \"\n\tOutput: \"Mr%2eJohn%2eSmith\"\n-----------------\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch01/PalindromePermutation.java[*Palindrome Permutation:*] Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words.\n\n[source,java]\n-----------------\n\tEXAMPLE\n\tInput: Tact Coa\n\tOutput: True (permutations:\"taco cat'; \"atco cta'; etc.)\n-----------------\n\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch01/OneEditAway.java[*One Away:*] There are three types of edits that can be performed on strings: insert a character, remove a character, or replace a character. Given two strings, write a function to check if they are one edit (or zero edits) away.\n\n[source,java]\n-----------------\nEXAMPLE\npale, ple -\u003e true\npales, pale -\u003e true\npale, bale -\u003e true\npale, bae -\u003e false\n-----------------\n\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch01/StringCompression.java[*String Compression:*] Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the\n\"compressed\"string would not become smaller than the original string, your method should return the original string. You can assume the string has only uppercase and lowercase letters (a - z).\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch01/RotateMatrix.java[*Rotate Matrix:*] Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place? \n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch01/ZeroMatrix.java[*Zero Matrix:*] Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch01/StringRotation.java[*String Rotation:*] Assume you have a method i5Substring which checks ifone word is a substring of another. Given two strings, 51 and 52, write code to check if 52 is a rotation of 51 using only one call to isSubstring \n\n[source,java]\n-----------------\ne.g., Uwaterbottleu is a rotation of uerbottlewatU.\n-----------------\n\n== https://github.com/sunilsoni/cracking-the-coding-interview/tree/master/src/com/ctci6/ch02[Solutions to Chapter 2: Linked List]\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch02/RemoveDups.java[*Remove Dups:*] Write code to remove duplicates from an unsorted linked list.\nFOLLOW UP:: How would you solve this problem if a temporary buffer is not allowed?\n\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch02/ReturnKthToLast.java[*Return Kth to Last:*] Implement an algorithm to find the kth to last element of a singly linked list.\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch02/DeleteMiddleNode.java[*Delete Middle Node:*] \nImplement an algorithm to delete a node in the middle (i.e., any node but the first and last node, not necessarily the exact middle) of a  singly linked list, given only access to that node.\n \n[source,java]\n-----------------\nEXAMPLE\nInput: the node c from the linked list a - \u003eb- \u003ec - \u003ed - \u003ee- \u003ef\nResult: nothing is returned, but the new linked list looks like a-\u003eb-\u003ed-\u003ee-\u003ef\n-----------------\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch02/Partition.java[*Partition:*] \nWrite code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater than or equal to x.\nIf x is contained within the list, the values of x only need to be after the elements less than x (see below).\nThe partition element x can appear anywhere in the \"right partition\"; it does not need to appear between the left and right partitions.\n\n[source,java]\n-----------------\nEXAMPLE\nInput: 3 -\u003e 5 -\u003e 8 -\u003e 5 -\u003e 113 -\u003e 2 -\u003e 1 [partition = 5]\nOutput: 3 -\u003e 1 -\u003e 2 -\u003e 113 -\u003e 5 -\u003e 5 -\u003e 8\n-----------------\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch02/SumLists.java[*Sum Lists:*] \nYou have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order,\nsuch that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.\n\n[source,java]\n-----------------\nEXAMPLE:\nInput: (7-\u003e 1 -\u003e 6) + (5 -\u003e 9 -\u003e 2) .That is,617 + 295.\nOutput: 2 - \u003e 1 - \u003e 9.That is, 912.\n-----------------\n\n[source,java]\n-----------------\nFOLLOW UP:Suppose the digits are stored in forward order. Repeat the above problem.\nInput: (6 -\u003e 1 -\u003e 7) + (2 -\u003e 9 -\u003e 5).That is,617 + 295.\nOutput: 9 - \u003e 1 - \u003e 2.That is, 912.\n-----------------\n\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch02/Palindrome.java[*Palindrome:*] \nImplement a function to check if a linked list is a palindrome.\n\n\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch02/Intersection.java[*Intersection:*] \nGiven two (singly) linked lists, determine if the two lists intersect. Return the intersecting node. Note that the intersection is defined based on reference, not value. That is, if the kth node of the first linked list is the exact same node (by reference) as the jth node of the second\nlinked list, then they are intersecting.\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch02/LoopDetection.java[*Loop Detection:*] \nGiven a circular linked list, implement an algorithm that returns the node at the beginning of the loop.\n\nDEFINITION:\nCircular linked list: A (corrupt) linked list in which a node's next pointer points to an earlier node, so as to make a loop in the linked list.\n\n[source,java]\n-----------------\nEXAMPLE\nInput:A - \u003e B - \u003e C - \u003e D - \u003e E - \u003e C [the same C as earlier]\nOutput:C\n-----------------\n\n\n\n== https://github.com/sunilsoni/cracking-the-coding-interview/tree/master/src/com/ctci6/ch03[Solutions to Chapter 3: Stacks and Queues]\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch03/ThreeInOneFixedMultiStack.java[*Three In One: Fixed Multi Stack*] \nDescribe how you could use a single array to implement three stacks:\n\n[source,java]\n-----------------\nApproach 1: Fixed Division\nWe can divide the array in three equal parts and allow the individual stack to grow in that limited space.\nNote: We will use the notation \"[\" to mean inclusive of an end point and \"(\" to mean exclusive of an end\npoint.\nFor stack 1, we will use [0, X).\nFor stack 2, we will use [ X, 2X) .\nFor stack 3, we will use [ 2X , n) .\n-----------------\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch03/ThreeInOneMultiStack.java[*Three In One: Multi Stack*] \nDescribe how you could use a single array to implement three stacks.\n\n[source,java]\n-----------------\nApproach 2: Flexible Divisions\nA second approach is to allow the stack blocks to be flexible in size. When one stack exceeds its initial\ncapacity, we grow the allowable capacity and shift elements as necessary.\n-----------------\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch03/StackWithMin.java[*Stack Min 1:*] \nHow would you design a stack which, in addition to push and pop, has a function min which returns the minimum element? Push, pop and min should all operate in 0(1) time.\n\n[source,java]\n-----------------\nApproach 1: One solution is to have just a single int value, minValue, that's a member of the Stack class. When\nminValue is popped from the stack, we search through the stack to find the new minimum. Unfortunately,\nthis would break the constraint that push and pop operate in 0(1) time.\n-----------------\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch03/StackWithMin2.java[*Stack Min 2:*] \nHow would you design a stack which, in addition to push and pop, has a function min which returns the minimum element? Push, pop and min should all operate in 0(1) time.\n\n[source,java]\n-----------------\nApproach 2: We can (maybe) do a bit better than this by using an additional stack which keeps track of the mins.\nWhy might this be more space efficient? Suppose we had a very large stack and the first element inserted\nhappened to be the minimum. In the first solution, we would be keeping n integers, where n is the size of\nthe stack. In the second solution though, we store just a few pieces of data: a second stack with one element\nand the members within this stack.\n-----------------\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch03/SetOfStacks.java[*Stack of Plates:*]\nImagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in real life, we would likely start a new stack when the previous stack exceeds some\nthreshold. Implement a data structure SetOfStacks that mimics this. SetOfStacks should be composed of several stacks and should create a new stack once the previous one exceeds capacity.\nSetOfStacks.push() and SetOfStacks. pop() should behave identically to a single stack (that is, pop() should return the same values as it would if there were just a single stack).\n\nFOLLOW UP:Implement a function popAt(int index) which performs a pop operation on a specifc substack.\n\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch03/QueueViaStack.java[*Queue Via Stack:*]\nImplement a MyQueue class which implements a queue using two stacks\n\n* https://github.com/sunilsoni/cracking-the-coding-interview/blob/master/src/com/ctci6/ch03/SortStack.java[*Sort Stack:*]\nWrite a program to sort a stack such that the smallest items are on the top. You can use an additional temporary stack, but you may not copy the elements into any other data structure (such as an array). The stack supports the following operations: push, pop, peek, and isEmpty.\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsunilsoni%2Fcracking-the-coding-interview","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsunilsoni%2Fcracking-the-coding-interview","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsunilsoni%2Fcracking-the-coding-interview/lists"}