{"id":2353,"date":"2026-05-27T18:20:26","date_gmt":"2026-05-27T18:20:26","guid":{"rendered":"https:\/\/tucumandevelopers.com\/index.php\/2026\/05\/27\/stacks-en-entrevistas-tecnicas-3-problemas-resueltos-paso-a-paso\/"},"modified":"2026-05-27T18:20:26","modified_gmt":"2026-05-27T18:20:26","slug":"stacks-en-entrevistas-tecnicas-3-problemas-resueltos-paso-a-paso","status":"publish","type":"post","link":"https:\/\/tucumandevelopers.com\/index.php\/2026\/05\/27\/stacks-en-entrevistas-tecnicas-3-problemas-resueltos-paso-a-paso\/","title":{"rendered":"Stacks en entrevistas t\u00e9cnicas: 3 problemas resueltos paso a paso"},"content":{"rendered":"<div>\n<div><\/div>\n<\/details>\n<h3> <a name=\"c%C3%B3mo-lo-pensamos\" href=\"#c%C3%B3mo-lo-pensamos\"> <\/a> \u00bfC\u00f3mo lo pensamos? <\/h3>\n<p>&#8220;El \u00faltimo que abri\u00f3 es el primero que debe cerrarse.&#8221; Justo eso es lo que un stack hace bien.<\/p>\n<p>Recorremos el string. Cada apertura va al stack. Cada cierre debe coincidir con el tope. Si al final el stack queda vac\u00edo, todo cerr\u00f3 bien.<\/p>\n<p><a href=\"https:\/\/media2.dev.to\/dynamic\/image\/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto\/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fse53h0183nqf2fo22ius.png\"><\/a><\/p>\n<h3> <a name=\"soluci%C3%B3n\" href=\"#soluci%C3%B3n\"> <\/a> Soluci\u00f3n <\/h3>\n<div>\n<pre><code><span>function<\/span> <span>validParentheses<\/span><span>(<\/span><span>s<\/span><span>)<\/span> <span>{<\/span> <span>const<\/span> <span>stack<\/span> <span>=<\/span> <span>[];<\/span> <span>const<\/span> <span>pairs<\/span> <span>=<\/span> <span>{<\/span> <span>\"<\/span><span>)<\/span><span>\"<\/span><span>:<\/span> <span>\"<\/span><span>(<\/span><span>\"<\/span><span>,<\/span> <span>\"<\/span><span>}<\/span><span>\"<\/span><span>:<\/span> <span>\"<\/span><span>{<\/span><span>\"<\/span><span>,<\/span> <span>\"<\/span><span>]<\/span><span>\"<\/span><span>:<\/span> <span>\"<\/span><span>[<\/span><span>\"<\/span> <span>};<\/span> <span>for <\/span><span>(<\/span><span>const<\/span> <span>char<\/span> <span>of<\/span> <span>s<\/span><span>)<\/span> <span>{<\/span> <span>if <\/span><span>(<\/span><span>char<\/span> <span>===<\/span> <span>\"<\/span><span>(<\/span><span>\"<\/span> <span>||<\/span> <span>char<\/span> <span>===<\/span> <span>\"<\/span><span>{<\/span><span>\"<\/span> <span>||<\/span> <span>char<\/span> <span>===<\/span> <span>\"<\/span><span>[<\/span><span>\"<\/span><span>)<\/span> <span>{<\/span> <span>stack<\/span><span>.<\/span><span>push<\/span><span>(<\/span><span>char<\/span><span>);<\/span> <span>}<\/span> <span>else<\/span> <span>if <\/span><span>(<\/span><span>stack<\/span><span>.<\/span><span>pop<\/span><span>()<\/span> <span>!==<\/span> <span>pairs<\/span><span>[<\/span><span>char<\/span><span>])<\/span> <span>{<\/span> <span>return<\/span> <span>false<\/span><span>;<\/span> <span>}<\/span> <span>}<\/span> <span>return<\/span> <span>stack<\/span><span>.<\/span><span>length<\/span> <span>===<\/span> <span>0<\/span><span>;<\/span> <span>}<\/span> <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<p>Lo importante:<\/p>\n<ol>\n<li> <code>pairs<\/code> mapea cada cierre con su apertura.<\/li>\n<li>Aperturas van al stack. Cierres hacen <code>pop<\/code> y validan.<\/li>\n<li>Si el stack est\u00e1 vac\u00edo al hacer <code>pop<\/code>, devuelve <code>undefined<\/code> y la comparaci\u00f3n falla. C\u00f3modo, as\u00ed no necesitamos un chequeo extra.<\/li>\n<li>Al final, el stack debe estar vac\u00edo.<\/li>\n<\/ol>\n<blockquote>\n<p>Complejidad: O(n) tiempo y O(n) espacio.<\/p>\n<\/blockquote>\n<h2> <a name=\"problema-2-reverse-string-easy\" href=\"#problema-2-reverse-string-easy\"> <\/a> Problema 2: Reverse String (easy) <\/h2>\n<p>Adaptaci\u00f3n de <a href=\"https:\/\/leetcode.com\/problems\/reverse-string\/description\/\" target=\"_blank\" rel=\"noopener noreferrer\">&#8220;Reverse String&#8221; de LeetCode<\/a>. Vamos a invertir una palabra usando stack.<\/p>\n<p>Problema: dado un string <code>s<\/code>, devuelve el string invertido.<\/p>\n<details>\n<summary>Ejemplos<\/summary>\n<p> <\/p>\n<div>\n<pre><code>Input: \"stack\" \u2192 \"kcats\" Input: \"hello\" \u2192 \"olleh\" <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<\/details>\n<h3> <a name=\"c%C3%B3mo-lo-pensamos\" href=\"#c%C3%B3mo-lo-pensamos\"> <\/a> \u00bfC\u00f3mo lo pensamos? <\/h3>\n<p>&#8220;Invertir&#8221; es la pista. Metes los elementos en orden y los sacas en orden inverso. Eso es exactamente lo que hace un stack.<\/p>\n<p>En la vida real usar\u00edas <code>s.split(\"\").reverse().join(\"\")<\/code> y listo. Aqu\u00ed lo hacemos con stack para ver el patr\u00f3n en acci\u00f3n.<\/p>\n<p><a href=\"https:\/\/media2.dev.to\/dynamic\/image\/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto\/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5vk6wkdys44zopqapji8.png\"><\/a><\/p>\n<h3> <a name=\"soluci%C3%B3n\" href=\"#soluci%C3%B3n\"> <\/a> Soluci\u00f3n <\/h3>\n<div>\n<pre><code><span>var<\/span> <span>reverseString<\/span> <span>=<\/span> <span>function <\/span><span>(<\/span><span>s<\/span><span>)<\/span> <span>{<\/span> <span>const<\/span> <span>stack<\/span> <span>=<\/span> <span>[...<\/span><span>s<\/span><span>];<\/span> <span>\/\/ crea un stack con los caracteres<\/span> <span>let<\/span> <span>reversed<\/span> <span>=<\/span> <span>\"\"<\/span><span>;<\/span> <span>while <\/span><span>(<\/span><span>stack<\/span><span>.<\/span><span>length<\/span> <span>&gt;<\/span> <span>0<\/span><span>)<\/span> <span>{<\/span> <span>reversed<\/span> <span>+=<\/span> <span>stack<\/span><span>.<\/span><span>pop<\/span><span>();<\/span> <span>}<\/span> <span>return<\/span> <span>reversed<\/span><span>;<\/span> <span>};<\/span> <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<p>Metemos todos los caracteres al stack y los vamos sacando uno por uno. Como <code>pop<\/code> devuelve el \u00faltimo que entr\u00f3, los caracteres salen al rev\u00e9s.<\/p>\n<blockquote>\n<p>Complejidad: O(n) tiempo y O(n) espacio.<\/p>\n<\/blockquote>\n<h2> <a name=\"problema-3-simplify-path-medium\" href=\"#problema-3-simplify-path-medium\"> <\/a> Problema 3: Simplify Path (medium) <\/h2>\n<p><a href=\"https:\/\/leetcode.com\/problems\/simplify-path\/description\/\" target=\"_blank\" rel=\"noopener noreferrer\">&#8220;Simplify Path&#8221; de LeetCode<\/a>.<\/p>\n<p>Problema: dada una ruta absoluta de Unix, convi\u00e9rtela a su forma can\u00f3nica.<\/p>\n<p>Reglas:<\/p>\n<ul>\n<li> <code>.<\/code> es el directorio actual.<\/li>\n<li> <code>..<\/code> sube un nivel.<\/li>\n<li> <code>\/\/<\/code> se trata como <code>\/<\/code>.<\/li>\n<li>El resultado no termina en <code>\/<\/code>, salvo la ra\u00edz.<\/li>\n<\/ul>\n<details>\n<summary>Ejemplos<\/summary>\n<p> <\/p>\n<div>\n<pre><code>Input: \"\/home\/\/foo\/\" \u2192 \"\/home\/foo\" Input: \"\/..\/\" \u2192 \"\/\" Input: \"\/a\/.\/b\/..\/..\/c\/\" \u2192 \"\/c\" <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<\/details>\n<h3> <a name=\"c%C3%B3mo-lo-pensamos\" href=\"#c%C3%B3mo-lo-pensamos\"> <\/a> \u00bfC\u00f3mo lo pensamos? <\/h3>\n<p>\u00bfQu\u00e9 hace <code>..<\/code>? Nos regresa al directorio anterior. Ah\u00ed est\u00e1 la se\u00f1al, necesitamos recordar por d\u00f3nde pasamos y poder retroceder.<\/p>\n<p>Partimos la ruta por <code>\/<\/code> y recorremos cada componente:<\/p>\n<ul>\n<li> <code>\"\"<\/code> o <code>\".\"<\/code>, ignora.<\/li>\n<li> <code>\"..\"<\/code>, saca el tope del stack.<\/li>\n<li>Cualquier otra cosa es un directorio y va al stack.<\/li>\n<\/ul>\n<p>Al final, el stack contiene los directorios de la ruta simplificada.<\/p>\n<p><a href=\"https:\/\/media2.dev.to\/dynamic\/image\/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto\/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flc69jgaauw38rep36yld.png\"><\/a><\/p>\n<h3> <a name=\"soluci%C3%B3n\" href=\"#soluci%C3%B3n\"> <\/a> Soluci\u00f3n <\/h3>\n<div>\n<pre><code><span>var<\/span> <span>simplifyPath<\/span> <span>=<\/span> <span>function <\/span><span>(<\/span><span>path<\/span><span>)<\/span> <span>{<\/span> <span>const<\/span> <span>stack<\/span> <span>=<\/span> <span>[];<\/span> <span>for <\/span><span>(<\/span><span>const<\/span> <span>part<\/span> <span>of<\/span> <span>path<\/span><span>.<\/span><span>split<\/span><span>(<\/span><span>\"<\/span><span>\/<\/span><span>\"<\/span><span>))<\/span> <span>{<\/span> <span>if <\/span><span>(<\/span><span>part<\/span> <span>===<\/span> <span>\"\"<\/span> <span>||<\/span> <span>part<\/span> <span>===<\/span> <span>\"<\/span><span>.<\/span><span>\"<\/span><span>)<\/span> <span>continue<\/span><span>;<\/span> <span>if <\/span><span>(<\/span><span>part<\/span> <span>===<\/span> <span>\"<\/span><span>..<\/span><span>\"<\/span><span>)<\/span> <span>stack<\/span><span>.<\/span><span>pop<\/span><span>();<\/span> <span>else<\/span> <span>stack<\/span><span>.<\/span><span>push<\/span><span>(<\/span><span>part<\/span><span>);<\/span> <span>}<\/span> <span>return<\/span> <span>\"<\/span><span>\/<\/span><span>\"<\/span> <span>+<\/span> <span>stack<\/span><span>.<\/span><span>join<\/span><span>(<\/span><span>\"<\/span><span>\/<\/span><span>\"<\/span><span>);<\/span> <span>};<\/span> <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<p>Tip: en JavaScript, <code>pop<\/code> sobre un stack vac\u00edo no rompe nada, solo devuelve <code>undefined<\/code>. As\u00ed que si la ruta intenta subir m\u00e1s all\u00e1 de la ra\u00edz, no hace falta validaci\u00f3n extra.<\/p>\n<blockquote>\n<p>Complejidad: O(n) tiempo y O(n) espacio.<\/p>\n<\/blockquote>\n<h2> <a name=\"el-patr%C3%B3n-detr%C3%A1s-de-los-tres\" href=\"#el-patr%C3%B3n-detr%C3%A1s-de-los-tres\"> <\/a> El patr\u00f3n detr\u00e1s de los tres <\/h2>\n<p>Si los lees seguidos vas a notar lo mismo. Los tres resuelven el mismo problema de fondo, poder regresar a algo anterior.<\/p>\n<ul>\n<li>Balanced parentheses: recordar la \u00faltima apertura para validar el cierre.<\/li>\n<li>Reverse string: regresar al orden opuesto.<\/li>\n<li>Simplify path: <code>..<\/code> regresa un nivel.<\/li>\n<\/ul>\n<p>Ese es el superpoder del stack. Cuando un problema te pide recordar lo \u00faltimo, deshacer algo o procesar de atr\u00e1s hacia adelante, casi siempre la respuesta es stack.<\/p>\n<h2> <a name=\"stacks-m%C3%A1s-all%C3%A1-de-las-entrevistas\" href=\"#stacks-m%C3%A1s-all%C3%A1-de-las-entrevistas\"> <\/a> Stacks m\u00e1s all\u00e1 de las entrevistas <\/h2>\n<p>Los stacks no son trivia de entrevistas. El patr\u00f3n de &#8220;regresar&#8221; aparece por todos lados:<\/p>\n<ul>\n<li>El bot\u00f3n de regresar del navegador es un stack.<\/li>\n<li>El undo\/redo de tu editor tambi\u00e9n.<\/li>\n<li>Los call stacks de los lenguajes (por eso existen los stack overflow errors).<\/li>\n<li>Pipelines de datos que necesitan mantener contexto de lo \u00faltimo visto.<\/li>\n<\/ul>\n<h2> <a name=\"para-seguir-practicando\" href=\"#para-seguir-practicando\"> <\/a> Para seguir practicando <\/h2>\n<p>Problemas de LeetCode ordenados por dificultad:<\/p>\n<ol>\n<li> <a href=\"https:\/\/leetcode.com\/problems\/min-stack\/description\/\" target=\"_blank\" rel=\"noopener noreferrer\">Min Stack<\/a>, easy.<\/li>\n<li> <a href=\"https:\/\/leetcode.com\/problems\/baseball-game\/description\/\" target=\"_blank\" rel=\"noopener noreferrer\">Baseball Game<\/a>, easy.<\/li>\n<li> <a href=\"https:\/\/leetcode.com\/problems\/evaluate-reverse-polish-notation\/description\/\" target=\"_blank\" rel=\"noopener noreferrer\">Evaluate Reverse Polish Notation<\/a>, medium.<\/li>\n<li> <a href=\"https:\/\/leetcode.com\/problems\/daily-temperatures\/description\/\" target=\"_blank\" rel=\"noopener noreferrer\">Daily Temperatures<\/a>, medium. Es la intro al patr\u00f3n de monotonic stack.<\/li>\n<\/ol>\n<p>\u00bfCu\u00e1l te cost\u00f3 m\u00e1s? D\u00e9jamelo en los comentarios. A m\u00ed Simplify Path me hizo dar m\u00e1s vueltas para resolverlo.<\/p>\n<\/p><\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Fuente: <a href=\"https:\/\/dev.to\/aws\/stacks-en-entrevistas-tecnicas-3-problemas-resueltos-paso-a-paso-o6e\">Art\u00edculo original<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00bfC\u00f3mo lo pensamos? &#8220;El \u00faltimo que abri\u00f3 es el primero que debe cerrarse.&#8221; Justo eso es lo que un stack hace bien. Recorremos el string. Cada apertura va al stack. Cada cierre debe coincidir con el tope. Si al final el stack queda vac\u00edo, todo cerr\u00f3 bien. Soluci\u00f3n function validParentheses(s) { const stack = []; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2352,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[41],"tags":[],"class_list":["post-2353","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devto"],"jetpack_publicize_connections":[],"_links":{"self":[{"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/posts\/2353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/comments?post=2353"}],"version-history":[{"count":0,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/posts\/2353\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/media\/2352"}],"wp:attachment":[{"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/media?parent=2353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/categories?post=2353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/tags?post=2353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}